下面是详细讲解“python使用tkinter实现简单计算器”的完整攻略。
第一步:导入tkinter库
在开始编写计算器之前,需要先导入Tkinter库。可以使用以下代码导入:
from tkinter import *
第二步:创建主窗口和添加组件
在导入库之后,需要创建主窗口并为其添加所需组件。这个过程可以使用以下代码完成:
root = Tk()
root.title("计算器")
# 创建一个输入框
entry = Entry(root, width=35, borderwidth=5)
entry.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
# 创建按钮
button_1 = Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_2 = Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_3 = Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_4 = Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_5 = Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_6 = Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6))
button_7 = Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_8 = Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_9 = Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_0 = Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0))
button_add = Button(root, text="+", padx=39, pady=20, command=button_add)
button_subtract = Button(root, text="-", padx=41, pady=20, command=button_subtract)
button_multiply = Button(root, text="*", padx=40, pady=20, command=button_multiply)
button_divide = Button(root, text="/", padx=41, pady=20, command=button_divide)
button_clear = Button(root, text="清除", padx=79, pady=20, command=button_clear)
button_equal = Button(root, text="=", padx=91, pady=20, command=button_equal)
在这段代码中,我们创建了一个主窗口,并为其添加了一个输入框和所有需要用到的按钮。
第三步:定义按钮点击事件
下一步是为每个按钮定义单击事件。这可以使用以下代码完成:
# 按钮单击事件
def button_click(number):
current = entry.get()
entry.delete(0, END)
entry.insert(0, str(current) + str(number))
def button_add():
first_number = entry.get()
global f_num
global math
math = "addition"
f_num = int(first_number)
entry.delete(0, END)
def button_subtract():
first_number = entry.get()
global f_num
global math
math = "subtraction"
f_num = int(first_number)
entry.delete(0, END)
def button_multiply():
first_number = entry.get()
global f_num
global math
math = "multiplication"
f_num = int(first_number)
entry.delete(0, END)
def button_divide():
first_number = entry.get()
global f_num
global math
math = "division"
f_num = int(first_number)
entry.delete(0, END)
def button_clear():
entry.delete(0, END)
def button_equal():
second_number = entry.get()
entry.delete(0, END)
if math == "addition":
entry.insert(0, f_num + int(second_number))
elif math == "subtraction":
entry.insert(0, f_num - int(second_number))
elif math == "multiplication":
entry.insert(0, f_num * int(second_number))
elif math == "division":
entry.insert(0, f_num / int(second_number))
在这段代码中,我们定义了单击事件(也称为回调函数)来处理按钮的点击。这些回调函数将执行根据所点击的按钮执行的相应操作。
第四步:运行计算器
定义完回调函数之后,可以通过以下代码运行计算器:
# 创建按钮的布局
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)
button_0.grid(row=4, column=0)
button_clear.grid(row=4, column=1, columnspan=2)
button_add.grid(row=5, column=0)
button_equal.grid(row=5, column=1, columnspan=2)
button_subtract.grid(row=6, column=0)
button_multiply.grid(row=6, column=1)
button_divide.grid(row=6, column=2)
root.mainloop()
这行代码运行计算器并将其显示在屏幕上。
示例1
下面是示例1,它将演示如何使用Python Tkinter实现一个简单的计算器。
from tkinter import *
root = Tk()
root.title("计算器")
# 创建一个输入框
entry = Entry(root, width=35, borderwidth=5)
entry.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
# 创建按钮
button_1 = Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_2 = Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_3 = Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_4 = Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_5 = Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_6 = Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6))
button_7 = Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_8 = Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_9 = Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_0 = Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0))
button_add = Button(root, text="+", padx=39, pady=20, command=button_add)
button_subtract = Button(root, text="-", padx=41, pady=20, command=button_subtract)
button_multiply = Button(root, text="*", padx=40, pady=20, command=button_multiply)
button_divide = Button(root, text="/", padx=41, pady=20, command=button_divide)
button_clear = Button(root, text="清除", padx=79, pady=20, command=button_clear)
button_equal = Button(root, text="=", padx=91, pady=20, command=button_equal)
# 按钮单击事件
def button_click(number):
current = entry.get()
entry.delete(0, END)
entry.insert(0, str(current) + str(number))
def button_add():
first_number = entry.get()
global f_num
global math
math = "addition"
f_num = int(first_number)
entry.delete(0, END)
def button_subtract():
first_number = entry.get()
global f_num
global math
math = "subtraction"
f_num = int(first_number)
entry.delete(0, END)
def button_multiply():
first_number = entry.get()
global f_num
global math
math = "multiplication"
f_num = int(first_number)
entry.delete(0, END)
def button_divide():
first_number = entry.get()
global f_num
global math
math = "division"
f_num = int(first_number)
entry.delete(0, END)
def button_clear():
entry.delete(0, END)
def button_equal():
second_number = entry.get()
entry.delete(0, END)
if math == "addition":
entry.insert(0, f_num + int(second_number))
elif math == "subtraction":
entry.insert(0, f_num - int(second_number))
elif math == "multiplication":
entry.insert(0, f_num * int(second_number))
elif math == "division":
entry.insert(0, f_num / int(second_number))
# 创建按钮的布局
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)
button_0.grid(row=4, column=0)
button_clear.grid(row=4, column=1, columnspan=2)
button_add.grid(row=5, column=0)
button_equal.grid(row=5, column=1, columnspan=2)
button_subtract.grid(row=6, column=0)
button_multiply.grid(row=6, column=1)
button_divide.grid(row=6, column=2)
root.mainloop()
当你运行这个程序时,会看到计算器的界面。
示例2
下面是示例2,它演示了如何将计算器与其他代码集成起来。例如,可以修改按钮中的回调函数来对计算器执行其他操作,而不仅仅是简单的数学计算。
from tkinter import *
# 按钮单击事件
def button_click(number):
current = entry.get()
entry.delete(0, END)
entry.insert(0, str(current) + str(number))
def button_add():
first_number = entry.get()
global f_num
global math
math = "addition"
f_num = int(first_number)
entry.delete(0, END)
def button_subtract():
first_number = entry.get()
global f_num
global math
math = "subtraction"
f_num = int(first_number)
entry.delete(0, END)
def button_multiply():
first_number = entry.get()
global f_num
global math
math = "multiplication"
f_num = int(first_number)
entry.delete(0, END)
def button_divide():
first_number = entry.get()
global f_num
global math
math = "division"
f_num = int(first_number)
entry.delete(0, END)
def button_clear():
entry.delete(0, END)
def button_equal():
second_number = entry.get()
entry.delete(0, END)
if math == "addition":
entry.insert(0, f_num + int(second_number))
elif math == "subtraction":
entry.insert(0, f_num - int(second_number))
elif math == "multiplication":
entry.insert(0, f_num * int(second_number))
elif math == "division":
entry.insert(0, f_num / int(second_number))
# 创建一个主窗口
root = Tk()
root.title("计算器")
# 创建一个输入框
entry = Entry(root, width=35, borderwidth=5)
entry.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
# 创建按钮
button_1 = Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_2 = Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_3 = Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_4 = Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_5 = Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_6 = Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6))
button_7 = Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_8 = Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_9 = Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_0 = Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0))
button_add = Button(root, text="+", padx=39, pady=20, command=button_add)
button_subtract = Button(root, text="-", padx=41, pady=20, command=button_subtract)
button_multiply = Button(root, text="*", padx=40, pady=20, command=button_multiply)
button_divide = Button(root, text="/", padx=41, pady=20, command=button_divide)
button_clear = Button(root, text="清除", padx=79, pady=20, command=button_clear)
button_equal = Button(root, text="=", padx=91, pady=20, command=button_equal)
# 创建按钮的布局
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)
button_0.grid(row=4, column=0)
button_clear.grid(row=4, column=1, columnspan=2)
button_add.grid(row=5, column=0)
button_equal.grid(row=5, column=1, columnspan=2)
button_subtract.grid(row=6, column=0)
button_multiply.grid(row=6, column=1)
button_divide.grid(row=6, column=2)
# 在计算器外部将计算结果导入到另一个便签
result_label = Label(root, text="计算结果:")
result_label.grid(row=7, column=0)
result_text = Label(root, text="")
result_text.grid(row=7, column=1)
# 隐藏计算结果同步到另一个标签的功能
def button_equal():
second_number = entry.get()
entry.delete(0, END)
if math == "addition":
result = f_num + int(second_number)
entry.insert(0, result)
result_text.config(text=result)
elif math == "subtraction":
result = f_num - int(second_number)
entry.insert(0, result)
result_text.config(text=result)
elif math == "multiplication":
result = f_num * int(second_number)
entry.insert(0, result)
result_text.config(text=result)
elif math == "division":
result = f_num / int(second_number)
entry.insert(0, result)
result_text.config(text=result)
# 运行主循环
root.mainloop()
在这个示例中,我们添加了一个标签,用于在计算结果被计算时显示结果的值。我们还添加了一行代码,将计算结果同步到另一个标签(result_text)。现在,每当单击“=”按钮时,计算结果都将显示在entry框中和另一个标签中。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python使用tkinter实现简单计算器 - Python技术站