下面是关于Python Tkinter做计算器的完整攻略。
确认开发环境
要在Python中开发GUI应用程序,必须使用tkinter模块。大多数Python发行版都已经包含了Tk和tkinter,也可以使用pip来安装。确保安装了Python Tkinter的最新版本。
导入tkinter模块
在Python中,要使用tkinter,首先需要导入库:
import tkinter as tk
创建GUI应用程序
创建GUI应用程序时,需要创建一个主窗口。在tkinter中,可以使用Tk()来实现:
# 创建主窗口
root = tk.Tk()
root.title("Calculator")
添加显示屏幕
计算器需要一个显示屏幕来显示结果,可以使用tkinter中的Entry组件来实现。在这里创建Entry组件,设置为只读:
# 添加显示屏幕
e = tk.Entry(root, width=35, borderwidth=5,font=('Helvetica', 10, 'bold'),justify='right',state='readonly')
e.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
我们这里定义了显示屏幕(即Entry)的一些属性,包括宽度、边框宽度、字体、对齐方式等。
定义数值按钮和普通按钮
计算器有10个数字按钮,加减乘除符号以及小数点按钮。我们可以写一个函数来处理所有这些数值:
# 定义数值按钮和普通按钮
def button_click(number):
current = e.get()
e.configure(state='normal')
e.delete(0, tk.END)
e.insert(0, str(current) + str(number))
e.configure(state='readonly')
button_1 = tk.Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_1.grid(row=1, column=0)
button_2 = tk.Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_2.grid(row=1, column=1)
# 诸如此类,剩下的我就不一一列举了,可以自己补充
定义算法按钮
计算器还需要加减乘除符号等算法按钮。单击这些按钮后,它们应向显示器添加相应的符号,并准备进行计算。
# 定义加减乘除按钮
def button_add():
first_number = e.get()
global f_num
global math
math = "addition"
f_num = float(first_number)
e.delete(0, tk.END)
def button_subtract():
first_number = e.get()
global f_num
global math
math = "subtraction"
f_num = float(first_number)
e.delete(0, tk.END)
def button_multiply():
first_number = e.get()
global f_num
global math
math = "multiplication"
f_num = float(first_number)
e.delete(0, tk.END)
def button_divide():
first_number = e.get()
global f_num
global math
math = "division"
f_num = float(first_number)
e.delete(0, tk.END)
def button_clear():
e.delete(0, tk.END)
def button_equal():
second_number = e.get()
e.delete(0, tk.END)
if math == "addition":
e.insert(0, f_num + float(second_number))
elif math == "subtraction":
e.insert(0, f_num - float(second_number))
elif math == "multiplication":
e.insert(0, f_num * float(second_number))
elif math == "division":
e.insert(0, f_num / float(second_number))
将所有按钮添加到图形界面
最后,将所有按钮添加到主窗口中。
# 将所有按钮添加到图形界面
button_1 = tk.Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_1.grid(row=1, column=0)
button_2 = tk.Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_2.grid(row=1, column=1)
# 诸如此类,剩下的我就不一一列举了,可以自己补充
# 数学操作按钮
button_add = tk.Button(root, text="+", padx=39, pady=20, command=button_add)
button_add.grid(row=5, column=0)
button_subtract = tk.Button(root, text="-", padx=41, pady=20, command=button_subtract)
button_subtract.grid(row=4, column=0)
button_multiply = tk.Button(root, text="*", padx=40, pady=20, command=button_multiply)
button_multiply.grid(row=3, column=0)
button_divide = tk.Button(root, text="/", padx=41, pady=20, command=button_divide)
button_divide.grid(row=2, column=0)
button_clear = tk.Button(root, text="Clear", padx=79, pady=20, command=button_clear)
button_clear.grid(row=6, column=1, columnspan=2)
button_equal = tk.Button(root, text="=", padx=91, pady=20, command=button_equal)
button_equal.grid(row=6, column=3, columnspan=2)
示例
下面是一个完整的计算器程序示例:
import tkinter as tk
def button_click(number):
current = e.get()
e.configure(state='normal')
e.delete(0, tk.END)
e.insert(0, str(current) + str(number))
e.configure(state='readonly')
def button_add():
first_number = e.get()
global f_num
global math
math = "addition"
f_num = float(first_number)
e.delete(0, tk.END)
def button_subtract():
first_number = e.get()
global f_num
global math
math = "subtraction"
f_num = float(first_number)
e.delete(0, tk.END)
def button_multiply():
first_number = e.get()
global f_num
global math
math = "multiplication"
f_num = float(first_number)
e.delete(0, tk.END)
def button_divide():
first_number = e.get()
global f_num
global math
math = "division"
f_num = float(first_number)
e.delete(0, tk.END)
def button_clear():
e.delete(0, tk.END)
def button_equal():
second_number = e.get()
e.delete(0, tk.END)
if math == "addition":
e.insert(0, f_num + float(second_number))
elif math == "subtraction":
e.insert(0, f_num - float(second_number))
elif math == "multiplication":
e.insert(0, f_num * float(second_number))
elif math == "division":
e.insert(0, f_num / float(second_number))
# 创建主窗口
root = tk.Tk()
root.title("Calculator")
# 添加显示屏幕
e = tk.Entry(root, width=35, borderwidth=5,font=('Helvetica', 10, 'bold'),justify='right',state='readonly')
e.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# 数值按钮和普通按钮
button_1 = tk.Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_1.grid(row=1, column=0)
button_2 = tk.Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_2.grid(row=1, column=1)
button_3 = tk.Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_3.grid(row=1, column=2)
button_4 = tk.Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_4.grid(row=2, column=0)
button_5 = tk.Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_5.grid(row=2, column=1)
button_6 = tk.Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6))
button_6.grid(row=2, column=2)
button_7 = tk.Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_7.grid(row=3, column=0)
button_8 = tk.Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_8.grid(row=3, column=1)
button_9 = tk.Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_9.grid(row=3, column=2)
button_0 = tk.Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0))
button_0.grid(row=4, column=0)
button_dot = tk.Button(root, text=".", padx=42, pady=20, command=lambda: button_click("."))
button_dot.grid(row=4, column=1)
# 数学操作按钮
button_add = tk.Button(root, text="+", padx=39, pady=20, command=button_add)
button_add.grid(row=5, column=0)
button_subtract = tk.Button(root, text="-", padx=41, pady=20, command=button_subtract)
button_subtract.grid(row=4, column=0)
button_multiply = tk.Button(root, text="*", padx=40, pady=20, command=button_multiply)
button_multiply.grid(row=3, column=0)
button_divide = tk.Button(root, text="/", padx=41, pady=20, command=button_divide)
button_divide.grid(row=2, column=0)
# 其他按钮
button_clear = tk.Button(root, text="Clear", padx=79, pady=20, command=button_clear)
button_clear.grid(row=6, column=1, columnspan=2)
button_equal = tk.Button(root, text="=", padx=91, pady=20, command=button_equal)
button_equal.grid(row=6, column=3, columnspan=2)
root.mainloop()
此计算器程序可以将所有数字、小数点和算法组合在一起以进行计算,而且界面布局也相当简洁梳理。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python tkinter 做个简单的计算器的方法 - Python技术站