用tkinter做一个计算器
时间: 2023-09-21 10:07:44 浏览: 157
基于python tk模块制作的计算器
好的,下面是一个简单的计算器示例,使用tkinter库实现GUI界面:
```python
from tkinter import *
class Calculator:
def __init__(self, master):
self.master = master
master.title("Calculator")
# 创建显示屏
self.display = Entry(master, width=25, font=('Arial', 18), justify='right')
self.display.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
# 创建按钮
self.button_1 = Button(master, text="1", width=5, height=2, command=lambda: self.button_click('1'))
self.button_2 = Button(master, text="2", width=5, height=2, command=lambda: self.button_click('2'))
self.button_3 = Button(master, text="3", width=5, height=2, command=lambda: self.button_click('3'))
self.button_4 = Button(master, text="4", width=5, height=2, command=lambda: self.button_click('4'))
self.button_5 = Button(master, text="5", width=5, height=2, command=lambda: self.button_click('5'))
self.button_6 = Button(master, text="6", width=5, height=2, command=lambda: self.button_click('6'))
self.button_7 = Button(master, text="7", width=5, height=2, command=lambda: self.button_click('7'))
self.button_8 = Button(master, text="8", width=5, height=2, command=lambda: self.button_click('8'))
self.button_9 = Button(master, text="9", width=5, height=2, command=lambda: self.button_click('9'))
self.button_0 = Button(master, text="0", width=5, height=2, command=lambda: self.button_click('0'))
self.button_add = Button(master, text="+", width=5, height=2, command=lambda: self.button_click('+'))
self.button_subtract = Button(master, text="-", width=5, height=2, command=lambda: self.button_click('-'))
self.button_multiply = Button(master, text="*", width=5, height=2, command=lambda: self.button_click('*'))
self.button_divide = Button(master, text="/", width=5, height=2, command=lambda: self.button_click('/'))
self.button_equal = Button(master, text="=", width=5, height=2, command=self.calculate)
self.button_clear = Button(master, text="C", width=5, height=2, command=self.clear_display)
# 放置按钮
self.button_1.grid(row=3, column=0)
self.button_2.grid(row=3, column=1)
self.button_3.grid(row=3, column=2)
self.button_add.grid(row=3, column=3)
self.button_4.grid(row=2, column=0)
self.button_5.grid(row=2, column=1)
self.button_6.grid(row=2, column=2)
self.button_subtract.grid(row=2, column=3)
self.button_7.grid(row=1, column=0)
self.button_8.grid(row=1, column=1)
self.button_9.grid(row=1, column=2)
self.button_multiply.grid(row=1, column=3)
self.button_0.grid(row=4, column=0)
self.button_clear.grid(row=4, column=1)
self.button_equal.grid(row=4, column=2)
self.button_divide.grid(row=4, column=3)
# 初始化计算器状态
self.equation = ""
self.result = False
def button_click(self, number):
# 在显示屏上添加数字
self.equation += str(number)
self.display.delete(0, END)
self.display.insert(0, self.equation)
def clear_display(self):
# 清空显示屏
self.equation = ""
self.display.delete(0, END)
def calculate(self):
# 计算结果
self.result = eval(self.equation)
self.display.delete(0, END)
self.display.insert(0, self.result)
self.equation = ""
root = Tk()
my_calculator = Calculator(root)
root.mainloop()
```
运行代码,即可看到计算器的界面。可以通过点击按钮来输入数字和运算符,并在显示屏上显示结果。
阅读全文