Python图形化界面设计四则运算设计
时间: 2023-11-29 18:57:32 浏览: 112
好的,我可以介绍一下使用 Python 的 tkinter 模块来设计一个简单的四则运算计算器。
以下是代码示例:
```python
from tkinter import *
class Calculator:
def __init__(self, master):
self.master = master
master.title("Calculator")
# 创建显示结果的文本框
self.display = Entry(master, width=30, borderwidth=5)
self.display.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# 创建数字按钮
self.button_1 = Button(master, text="1", padx=40, pady=20, command=lambda: self.button_click(1))
self.button_2 = Button(master, text="2", padx=40, pady=20, command=lambda: self.button_click(2))
self.button_3 = Button(master, text="3", padx=40, pady=20, command=lambda: self.button_click(3))
self.button_4 = Button(master, text="4", padx=40, pady=20, command=lambda: self.button_click(4))
self.button_5 = Button(master, text="5", padx=40, pady=20, command=lambda: self.button_click(5))
self.button_6 = Button(master, text="6", padx=40, pady=20, command=lambda: self.button_click(6))
self.button_7 = Button(master, text="7", padx=40, pady=20, command=lambda: self.button_click(7))
self.button_8 = Button(master, text="8", padx=40, pady=20, command=lambda: self.button_click(8))
self.button_9 = Button(master, text="9", padx=40, pady=20, command=lambda: self.button_click(9))
self.button_0 = Button(master, text="0", padx=40, pady=20, command=lambda: self.button_click(0))
# 将数字按钮放置在界面上
self.button_1.grid(row=1, column=0)
self.button_2.grid(row=1, column=1)
self.button_3.grid(row=1, column=2)
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_7.grid(row=3, column=0)
self.button_8.grid(row=3, column=1)
self.button_9.grid(row=3, column=2)
self.button_0.grid(row=4, column=1)
# 创建操作符按钮
self.button_add = Button(master, text="+", padx=39, pady=20, command=self.add)
self.button_subtract = Button(master, text="-", padx=41, pady=20, command=self.subtract)
self.button_multiply = Button(master, text="*", padx=40, pady=20, command=self.multiply)
self.button_divide = Button(master, text="/", padx=41, pady=20, command=self.divide)
# 将操作符按钮放置在界面上
self.button_add.grid(row=5, column=0)
self.button_subtract.grid(row=6, column=0)
self.button_multiply.grid(row=6, column=1)
self.button_divide.grid(row=6, column=2)
# 创建清空和等于按钮
self.button_clear = Button(master, text="Clear", padx=79, pady=20, command=self.clear)
self.button_equals = Button(master, text="=", padx=91, pady=20, command=self.equals)
# 将清空和等于按钮放置在界面上
self.button_clear.grid(row=4, column=0, columnspan=2)
self.button_equals.grid(row=4, column=2, columnspan=2)
# 创建变量用于存储数字和操作符
self.num1 = None
self.num2 = None
self.operator = None
# 按钮点击事件处理函数
def button_click(self, number):
current = self.display.get()
self.display.delete(0, END)
self.display.insert(0, str(current) + str(number))
# 操作符按钮处理函数
def add(self):
self.num1 = float(self.display.get())
self.operator = "+"
self.display.delete(0, END)
def subtract(self):
self.num1 = float(self.display.get())
self.operator = "-"
self.display.delete(0, END)
def multiply(self):
self.num1 = float(self.display.get())
self.operator = "*"
self.display.delete(0, END)
def divide(self):
self.num1 = float(self.display.get())
self.operator = "/"
self.display.delete(0, END)
# 清空按钮处理函数
def clear(self):
self.num1 = None
self.num2 = None
self.operator = None
self.display.delete(0, END)
# 等于按钮处理函数
def equals(self):
if self.operator is None:
return
self.num2 = float(self.display.get())
self.display.delete(0, END)
if self.operator == "+":
result = self.num1 + self.num2
elif self.operator == "-":
result = self.num1 - self.num2
elif self.operator == "*":
result = self.num1 * self.num2
elif self.operator == "/":
result = self.num1 / self.num2
self.display.insert(0, result)
# 创建主窗口
root = Tk()
# 创建计算器实例
calculator = Calculator(root)
# 运行主循环
root.mainloop()
```
这个计算器界面包括数字按钮、操作符按钮、清空按钮和等于按钮。通过点击数字按钮和操作符按钮,我们可以输入并存储数字和操作符。当点击等于按钮时,程序会根据存储的数字和操作符计算结果并显示在文本框中。
这只是一个简单的示例,你可以根据需要自行扩展和修改。
阅读全文