用python写一个计算器 并在标签上写姓名和学号
时间: 2023-08-14 18:02:11 浏览: 92
python课设全套资源,换上姓名学号即可,内有源代码,流程图,心得体会,任务设计,代码详解
好的,以下是一个简单的计算器代码示例,同时在标签上写上姓名和学号:
```python
import tkinter as tk
# 创建窗口
window = tk.Tk()
window.title("Calculator")
window.geometry("300x200")
# 添加姓名和学号标签
label = tk.Label(window, text="Name: YourName\nStudent ID: 123456")
label.pack()
# 计算器逻辑
def calculate():
num1 = float(entry1.get())
num2 = float(entry2.get())
operator = operator_var.get()
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
if num2 == 0:
result = "Error: cannot divide by zero"
else:
result = num1 / num2
result_label.config(text=str(result))
# 添加输入框和按钮
label1 = tk.Label(window, text="Number 1:")
label1.pack()
entry1 = tk.Entry(window)
entry1.pack()
label2 = tk.Label(window, text="Number 2:")
label2.pack()
entry2 = tk.Entry(window)
entry2.pack()
operator_var = tk.StringVar(value="+")
operator_radiobuttons = tk.Frame(window)
operator_radiobuttons.pack()
for operator in ["+", "-", "*", "/"]:
rb = tk.Radiobutton(operator_radiobuttons, text=operator, variable=operator_var, value=operator)
rb.pack(side="left")
calculate_button = tk.Button(window, text="Calculate", command=calculate)
calculate_button.pack()
# 添加结果标签
result_label = tk.Label(window, text="")
result_label.pack()
# 运行窗口
window.mainloop()
```
请将 `YourName` 和 `123456` 替换为自己的姓名和学号。
阅读全文