设计一个程序,用两个文本框输入数值数据,用列表框存放+、-、*、/、幂次方、余数。用户先输入两个操作数,再从列表框中选择一种运算,即可在标签中显示出计算结果
时间: 2023-11-27 18:53:58 浏览: 97
以下是一个简单的 Python Tkinter 程序,它实现了你所需要的功能:
```python
import tkinter as tk
def calculate():
num1 = float(entry1.get())
num2 = float(entry2.get())
operation = operations_listbox.get(operations_listbox.curselection())
if operation == "+":
result = num1 + num2
elif operation == "-":
result = num1 - num2
elif operation == "*":
result = num1 * num2
elif operation == "/":
result = num1 / num2
elif operation == "^":
result = num1 ** num2
elif operation == "%":
result = num1 % num2
result_label.config(text="Result: " + str(result))
root = tk.Tk()
root.title("Calculator")
entry1 = tk.Entry(root)
entry1.pack()
entry2 = tk.Entry(root)
entry2.pack()
operations_listbox = tk.Listbox(root)
operations_listbox.insert(1, "+")
operations_listbox.insert(2, "-")
operations_listbox.insert(3, "*")
operations_listbox.insert(4, "/")
operations_listbox.insert(5, "^")
operations_listbox.insert(6, "%")
operations_listbox.pack()
calculate_button = tk.Button(root, text="Calculate", command=calculate)
calculate_button.pack()
result_label = tk.Label(root)
result_label.pack()
root.mainloop()
```
程序的界面由两个文本框、一个列表框、一个按钮和一个标签组成。当用户点击 Calculate 按钮时,程序将获取两个输入的操作数和所选的运算符,然后根据选定的运算符执行相应的计算。结果将在标签中显示。
阅读全文