python实现带弹框加减乘除计算器
时间: 2023-10-14 16:05:01 浏览: 139
实现计算器加减乘除的功能
好的,我可以为您解答这个问题。以下是一个简单的 Python 代码,可以实现带弹框的加减乘除计算器:
```python
from tkinter import *
def calculate():
num1 = float(entry1.get())
num2 = float(entry2.get())
operator = option.get()
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
result = num1 / num2
else:
result = "Invalid operator"
messagebox.showinfo("Result", result)
root = Tk()
root.title("Calculator")
label1 = Label(root, text="Enter first number:")
label1.pack()
entry1 = Entry(root)
entry1.pack()
label2 = Label(root, text="Enter second number:")
label2.pack()
entry2 = Entry(root)
entry2.pack()
option = StringVar()
option.set("+")
add_radiobutton = Radiobutton(root, text="+", variable=option, value="+")
add_radiobutton.pack()
subtract_radiobutton = Radiobutton(root, text="-", variable=option, value="-")
subtract_radiobutton.pack()
multiply_radiobutton = Radiobutton(root, text="*", variable=option, value="*")
multiply_radiobutton.pack()
divide_radiobutton = Radiobutton(root, text="/", variable=option, value="/")
divide_radiobutton.pack()
button = Button(root, text="Calculate", command=calculate)
button.pack()
root.mainloop()
```
当您运行这个程序时,它会打开一个 GUI 界面,让您输入两个数字并选择要执行的操作。当您单击“Calculate”按钮时,它会执行所选的操作并在弹出窗口中显示结果。希望这个代码对您有所帮助!
阅读全文