python编写一个程序,实现简单计算器窗口,包括两个数基本的加减乘除运算
时间: 2024-05-07 14:21:47 浏览: 175
基于Python编写一个计算器程序,实现简单的加减乘除和取余二元运算
好的,以下是一个简单的Python程序,用于创建一个基本的计算器窗口,可以进行加减乘除运算:
```
from tkinter import *
class Calculator:
def __init__(self, master):
self.master = master
master.title("简单计算器")
# 创建文本框
self.display = Entry(master, width=35, borderwidth=5)
self.display.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# 创建按钮
button_1 = Button(master, text="1", padx=40, pady=20, command=lambda: self.button_click(1))
button_2 = Button(master, text="2", padx=40, pady=20, command=lambda: self.button_click(2))
button_3 = Button(master, text="3", padx=40, pady=20, command=lambda: self.button_click(3))
button_4 = Button(master, text="4", padx=40, pady=20, command=lambda: self.button_click(4))
button_5 = Button(master, text="5", padx=40, pady=20, command=lambda: self.button_click(5))
button_6 = Button(master, text="6", padx=40, pady=20, command=lambda: self.button_click(6))
button_7 = Button(master, text="7", padx=40, pady=20, command=lambda: self.button_click(7))
button_8 = Button(master, text="8", padx=40, pady=20, command=lambda: self.button_click(8))
button_9 = Button(master, text="9", padx=40, pady=20, command=lambda: self.button_click(9))
button_0 = Button(master, text="0", padx=40, pady=20, command=lambda: self.button_click(0))
button_add = Button(master, text="+", padx=39, pady=20, command=self.button_add)
button_subtract = Button(master, text="-", padx=41, pady=20, command=self.button_subtract)
button_multiply = Button(master, text="*", padx=40, pady=20, command=self.button_multiply)
button_divide = Button(master, text="/", padx=41, pady=20, command=self.button_divide)
button_clear = Button(master, text="清空", padx=79, pady=20, command=self.button_clear)
button_equal = Button(master, text="=", padx=91, pady=20, command=self.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_subtract.grid(row=6, column=0)
button_multiply.grid(row=6, column=1)
button_divide.grid(row=6, column=2)
button_equal.grid(row=5, column=1, columnspan=2)
self.first_number = None
self.operation = None
def button_click(self, number):
current = self.display.get()
self.display.delete(0, END)
self.display.insert(0, str(current) + str(number))
def button_clear(self):
self.display.delete(0, END)
self.first_number = None
self.operation = None
def button_add(self):
self.first_number = float(self.display.get())
self.operation = "add"
self.display.delete(0, END)
def button_subtract(self):
self.first_number = float(self.display.get())
self.operation = "subtract"
self.display.delete(0, END)
def button_multiply(self):
self.first_number = float(self.display.get())
self.operation = "multiply"
self.display.delete(0, END)
def button_divide(self):
self.first_number = float(self.display.get())
self.operation = "divide"
self.display.delete(0, END)
def button_equal(self):
second_number = float(self.display.get())
self.display.delete(0, END)
if self.operation == "add":
result = self.first_number + second_number
elif self.operation == "subtract":
result = self.first_number - second_number
elif self.operation == "multiply":
result = self.first_number * second_number
elif self.operation == "divide":
result = self.first_number / second_number
self.display.insert(0, result)
root = Tk()
my_calculator = Calculator(root)
root.mainloop()
```
首先,我们导入了`tkinter`模块,并创建了一个名为`Calculator`的类。然后,我们创建了一个窗口,并在其中添加了一个文本框和一组按钮。每个按钮都有一个命令,用于执行相应的操作。
该程序定义了以下方法:
- `button_click()`:添加按钮的单击事件,用于将数字添加到文本框中。
- `button_clear()`:清空文本框和操作。
- `button_add()`:将第一个数字存储在变量`first_number`中,并将操作设置为“add”。
- `button_subtract()`:将第一个数字存储在变量`first_number`中,并将操作设置为“subtract”。
- `button_multiply()`:将第一个数字存储在变量`first_number`中,并将操作设置为“multiply”。
- `button_divide()`:将第一个数字存储在变量`first_number`中,并将操作设置为“divide”。
- `button_equal()`:将第二个数字从文本框中获取,并根据操作执行相应的计算。然后,将结果添加到文本框中。
最后,我们创建了一个`Calculator`对象,并运行主循环,以便用户可以使用该计算器窗口。
阅读全文