找回密码
 中文实名注册
查看: 90|回复: 1

python作业

[复制链接]

3

主题

4

帖子

211

积分

中级会员

Rank: 3Rank: 3

积分
211
发表于 2025-1-25 09:34:03 | 显示全部楼层 |阅读模式
import tkinter as tk
from tkinter import messagebox

def 温暖不听讲():

    num1 = float(entry1.get())
    num2 = float(entry2.get())
    total = num1 + num2


    result_label.config(text=f"结果:{total}")


root = tk.Tk()
root.title("加法计算器")

root.geometry("400x300")
root.configure(bg='green')



label1 = tk.Label(root, text="请输入第一个数字:")
label1.pack()

entry1 = tk.Entry(root)
entry1.pack()

label2 = tk.Label(root, text="请输入第二个数字:")
label2.pack()

entry2 = tk.Entry(root)
entry2.pack()


add_button = tk.Button(root, text="计算和", command=温暖不听讲)
add_button.pack()




result_label = tk.Label(root, text="结果:")
result_label.pack()




root.mainloop()
回复

使用道具 举报

3

主题

4

帖子

211

积分

中级会员

Rank: 3Rank: 3

积分
211
 楼主| 发表于 2025-1-25 12:07:35 | 显示全部楼层
import tkinter as tk


def calculate():
    try:
        num1 = float(entry_num1.get())
        num2 = float(entry_num2.get())
        operator = selected_operator.get()

        if operator == '+':
            result = num1 + num2
        elif operator == '-':
            result = num1 - num2
        elif operator == '*':
            result = num1 * num2
        elif operator == '/':
            if num2 != 0:
                result = num1 / num2
            else:
                result = "错误:除数不能为 0"
        else:
            result = "无效的运算符"

        label_result.config(text=f"结果: {result}")

    except ValueError:
        label_result.config(text="错误:请输入有效的数字")


# 创建主窗口
root = tk.Tk()
root.title("简易计算器")

# 输入框和标签
entry_num1 = tk.Entry(root)
entry_num1.pack()

selected_operator = tk.StringVar(root, "+")
operators = ["+", "-", "*", "/"]
for operator in operators:
    rb = tk.Radiobutton(root, text=operator,
                        variable=selected_operator, value=operator)
    rb.pack()

entry_num2 = tk.Entry(root)
entry_num2.pack()

# 计算按钮
btn_calculate = tk.Button(root, text="计算", command=calculate)
btn_calculate.pack()

# 结果标签
label_result = tk.Label(root, text="结果: ")
label_result.pack()

# 运行主循环
root.mainloop()
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

小黑屋|东台市机器人学会 ( 苏ICP备2021035350号-1;苏ICP备2021035350号-2;苏ICP备2021035350号-3 )

GMT+8, 2025-3-14 04:56 , Processed in 0.038469 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表