设计并实现一个类似于windows操作系统附件中自带的计算器的一款简单的计算器,要求界面美观,设计合理;带有常用的菜单并实现其功能;能完成加、减、乘、除等简单的计算,在下面写出其核心代码。
时间: 2024-05-08 10:18:52 浏览: 68
好的,以下是一个简单的计算器的核心代码示例:
```python
from tkinter import *
# 创建GUI窗口
root = Tk()
root.title("简易计算器")
# 创建输入框和按钮
display = Entry(root, width=40, borderwidth=5)
display.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
def button_click(number):
current = display.get()
display.delete(0, END)
display.insert(0, str(current) + str(number))
def button_clear():
display.delete(0, END)
def button_add():
first_number = display.get()
global f_num
global math
math = "addition"
f_num = int(first_number)
display.delete(0, END)
def button_subtract():
first_number = display.get()
global f_num
global math
math = "subtraction"
f_num = int(first_number)
display.delete(0, END)
def button_multiply():
first_number = display.get()
global f_num
global math
math = "multiplication"
f_num = int(first_number)
display.delete(0, END)
def button_divide():
first_number = display.get()
global f_num
global math
math = "division"
f_num = int(first_number)
display.delete(0, END)
def button_equal():
second_number = display.get()
display.delete(0, END)
if math == "addition":
display.insert(0, f_num + int(second_number))
elif math == "subtraction":
display.insert(0, f_num - int(second_number))
elif math == "multiplication":
display.insert(0, f_num * int(second_number))
elif math == "division":
display.insert(0, f_num / int(second_number))
# 创建按钮
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_equal = Button(root, text="=", padx=91, pady=20, command=button_equal)
button_clear = Button(root, text="Clear", padx=79, pady=20, command=button_clear)
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_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()
```
该示例使用Tkinter模块创建了一个简单的GUI窗口,包括一个输入框和一些数学操作按钮。使用lambda函数和按钮的命令参数,使得单击按钮时可以执行指定的函数。在这个示例中,点击数字按钮将数字添加到输入框中;点击加、减、乘、除按钮将执行相应的数学运算;点击“Clear”按钮将清空输入框;点击等号按钮将计算并显示结果。注意:该代码并不是完整的程序。界面美观以及其他功能实现需要自行编写,以上代码仅作为核心代码示例。
阅读全文