亲宝软件园·资讯

展开

Python实现简易计算器的示例代码

羊必瘦 人气:2

上次我用我学习的python做一个简易的计算器,我对计算器进行了,更改优化,变成了一个真正的计算器

实现流程

1.计算机布局

2.计算机执行

首先导入模块:

Tkinter 作为 Python GUI 开发工具之一,它具有 GUI 软件包的必备的常用功能。比如,它提供了十多种不同类型的窗口控件、窗口布局管理器、事件处理机制等,加之其开发效率高、代码简洁易读

import tkinter as tk
#Python3标准安装包中自带tkinter,即不用安装,导入即可使用。
#可以从命令行运行python -m tkinter会打开一个窗口
#演示一个简单的Tk接口,证明tkinter已正确安装在系统上.
#python -m tkinter

计算器布局

1、先完成整个计算机的布局及其它输出结果的一栏

前面完成整体布局就细化计算器中的按钮

1、前四行代码是定义按钮的符号、长宽、字体、颜色

2、后四行是按钮的间距

root = tk.Tk()
root.title('计算器')
root.geometry('295x280+100+100') #边距
font = ('宋体', 20)
font_16 = ('宋体', 16)
root.attributes("-alpha", 0.9) #设置透明度
 
result_num = tk.StringVar() #赋予可变变量
result_num.set('')
 
tk.Label(root,
         textvariable=result_num, font=font, height=2,
         width=20, justify=tk.LEFT, anchor=tk.SE
         ).grid(row=1, column=1, columnspan=4) #显示结果的布局
 
 
button_clear = tk.Button(root, text='C', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') #定义按钮的符号、长宽、字体、颜色
button_back = tk.Button(root, text='←', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') #定义按钮的符号、长宽、字体、颜色
button_division = tk.Button(root, text='÷', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') #定义按钮的符号、长宽、字体、颜色
button_multiplication = tk.Button(root, text='×', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') #定义按钮的符号、长宽、字体、颜色
button_clear.grid(row=2, column=1, padx=4, pady=2)
button_back.grid(row=2, column=2, padx=4, pady=2)
button_division.grid(row=2, column=3, padx=4, pady=2)
button_multiplication.grid(row=2, column=4, padx=4, pady=2) #定义按钮位置
 
#依葫芦画瓢
button_seven = tk.Button(root, text='7', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_eight = tk.Button(root, text='8', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_nine = tk.Button(root, text='9', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_subtraction = tk.Button(root, text='-', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_seven.grid(row=3, column=1, padx=4, pady=2)
button_eight.grid(row=3, column=2, padx=4, pady=2)
button_nine.grid(row=3, column=3, padx=4, pady=2)
button_subtraction.grid(row=3, column=4, padx=4, pady=2)
 
#依葫芦画瓢
button_four = tk.Button(root, text='4', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_five = tk.Button(root, text='5', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_six = tk.Button(root, text='6', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_addition = tk.Button(root, text='+', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_four.grid(row=4, column=1, padx=4, pady=2)
button_five.grid(row=4, column=2, padx=4, pady=2)
button_six.grid(row=4, column=3, padx=4, pady=2)
button_addition.grid(row=4, column=4, padx=4, pady=2)
 
#依葫芦画瓢
button_one = tk.Button(root, text='1', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_two = tk.Button(root, text='2', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_three = tk.Button(root, text='3', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_equal = tk.Button(root, text='=', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_one.grid(row=5, column=1, padx=4, pady=2)
button_two.grid(row=5, column=2, padx=4, pady=2)
button_three.grid(row=5, column=3, padx=4, pady=2)
button_equal.grid(row=5, column=4, padx=4, pady=2)
 
 
button_zero1 = tk.Button(root, text=' ', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_zero = tk.Button(root, text='0', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_dot = tk.Button(root, text='.', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_equal2 = tk.Button(root, text=' ', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_zero1.grid(row=6, column=1, padx=4, pady=2)
button_zero.grid(row=6, column=2, padx=4, pady=2)
button_dot.grid(row=6, column=3, padx=4, pady=2)
button_equal2.grid(row=6, column=4, padx=4, pady=2)

计算机执行

计算器布局完成后(主体和细节) 赋予计算器计算能力

1、点击按钮实现出现结果输出栏及计算器的输出栏

2、赋予事件能力

def click_button(x):
    print('X:\t', x)
    result_num.set(result_num.get() + x)

点击按钮可以输入所对应的结果 并且调用lambda函数计算输入结果并反馈

ambda 函数是匿名的: 所谓匿名函数,通俗地说就是没有名字的函数。lambda函数没有名字。 lambda 函数有输入和输出: 输入是传入到参数列表argument_list的值,输出是根据表达式expression计算得到的值。

button_one.config(command=lambda: click_button('1'))
button_two.config(command=lambda: click_button('2'))
button_three.config(command=lambda: click_button('3'))
button_four.config(command=lambda: click_button('4'))
button_five.config(command=lambda: click_button('5'))
button_six.config(command=lambda: click_button('6'))
button_seven.config(command=lambda: click_button('7'))
button_eight.config(command=lambda: click_button('8'))
button_nine.config(command=lambda: click_button('9'))
button_zero.config(command=lambda: click_button('0'))
button_dot.config(command=lambda: click_button('.'))
button_addition.config(command=lambda: click_button('+'))
button_subtraction.config(command=lambda: click_button('-'))
button_multiplication.config(command=lambda: click_button('*'))
button_division.config(command=lambda: click_button('/'))
button_clear.config(command=lambda: btnClearDisplay())
button_equal.config(command=calculation)

调用calculation时间用来计算输入结果 其中str来转化字符串.

调用btnClearDisplay清除来清空输入结果

def calculation():
    opt_str = result_num.get()
    result = eval(opt_str)
    result_num.set(str(result))
 
 
def btnClearDisplay():
    opt_str = result_num.get()
    result = eval(str(opt_str))
    result_num.set("")

最后一行代码不需要触发lambda 因为不需要匿名函数也不用传递参数     

button_clear.config(command=lambda: btnClearDisplay())
button_equal.config(command=calculation)

代码展示

import tkinter as tk
 
root = tk.Tk()
root.title('计算器')
root.geometry('295x280+100+100')
font = ('宋体', 20)
font_16 = ('宋体', 16)
root.attributes("-alpha", 0.9)
 
result_num = tk.StringVar()
result_num.set('')
 
tk.Label(root,
         textvariable=result_num, font=font, height=2,
         width=20, justify=tk.LEFT, anchor=tk.SE
         ).grid(row=1, column=1, columnspan=4)
 
 
button_clear = tk.Button(root, text='C', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_back = tk.Button(root, text='←', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_division = tk.Button(root, text='÷', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_multiplication = tk.Button(root, text='×', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_clear.grid(row=2, column=1, padx=4, pady=2)
button_back.grid(row=2, column=2, padx=4, pady=2)
button_division.grid(row=2, column=3, padx=4, pady=2)
button_multiplication.grid(row=2, column=4, padx=4, pady=2)
 
 
button_seven = tk.Button(root, text='7', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_eight = tk.Button(root, text='8', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_nine = tk.Button(root, text='9', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_subtraction = tk.Button(root, text='-', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_seven.grid(row=3, column=1, padx=4, pady=2)
button_eight.grid(row=3, column=2, padx=4, pady=2)
button_nine.grid(row=3, column=3, padx=4, pady=2)
button_subtraction.grid(row=3, column=4, padx=4, pady=2)
 
button_four = tk.Button(root, text='4', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_five = tk.Button(root, text='5', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_six = tk.Button(root, text='6', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_addition = tk.Button(root, text='+', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_four.grid(row=4, column=1, padx=4, pady=2)
button_five.grid(row=4, column=2, padx=4, pady=2)
button_six.grid(row=4, column=3, padx=4, pady=2)
button_addition.grid(row=4, column=4, padx=4, pady=2)
 
 
button_one = tk.Button(root, text='1', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_two = tk.Button(root, text='2', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_three = tk.Button(root, text='3', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_equal = tk.Button(root, text='=', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_one.grid(row=5, column=1, padx=4, pady=2)
button_two.grid(row=5, column=2, padx=4, pady=2)
button_three.grid(row=5, column=3, padx=4, pady=2)
button_equal.grid(row=5, column=4, padx=4, pady=2)
 
 
button_zero1 = tk.Button(root, text=' ', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_zero = tk.Button(root, text='0', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_dot = tk.Button(root, text='.', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_equal2 = tk.Button(root, text=' ', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_zero1.grid(row=6, column=1, padx=4, pady=2)
button_zero.grid(row=6, column=2, padx=4, pady=2)
button_dot.grid(row=6, column=3, padx=4, pady=2)
button_equal2.grid(row=6, column=4, padx=4, pady=2)
 
 
def click_button(x):
    print('X:\t', x)
    result_num.set(result_num.get() + x)
 
 
def calculation():
    opt_str = result_num.get()
    result = eval(opt_str)
    result_num.set(str(result))
 
 
def btnClearDisplay():
    opt_str = result_num.get()
    result = eval(str(opt_str))
    result_num.set("")
 
 
button_one.config(command=lambda: click_button('1'))
button_two.config(command=lambda: click_button('2'))
button_three.config(command=lambda: click_button('3'))
button_four.config(command=lambda: click_button('4'))
button_five.config(command=lambda: click_button('5'))
button_six.config(command=lambda: click_button('6'))
button_seven.config(command=lambda: click_button('7'))
button_eight.config(command=lambda: click_button('8'))
button_nine.config(command=lambda: click_button('9'))
button_zero.config(command=lambda: click_button('0'))
button_dot.config(command=lambda: click_button('.'))
button_addition.config(command=lambda: click_button('+'))
button_subtraction.config(command=lambda: click_button('-'))
button_multiplication.config(command=lambda: click_button('*'))
button_division.config(command=lambda: click_button('/'))
button_clear.config(command=lambda: btnClearDisplay())
button_equal.config(command=calculation)
 
 
root.mainloop()

运行展示

可以看到计算器的页面特别好看,跟着我一起学python,成就感满满 !!!

加载全部内容

相关教程
猜你喜欢
用户评论