You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将计算结果输出到Tkinter GUI界面而非终端?

问题描述

我刚开始学习编程,恳请各位提供帮助!我希望将计算结果显示在GUI界面上,我了解当前print语句会直接输出到终端,但不清楚如何修改实现该需求。以下是我的Tkinter代码:

from tkinter import *

window = Tk() #instantiate an instance of a window
window.geometry("360x360")
window.title("Gold River 2.0")

window.config(background="#181818")


#enter weekly earnings
earnings = Entry()
earnings.pack()

#calculate button

def calculate():
    total = earnings.get()
    print("$",total,"earnt")

    free = int(total) * 0.25
    print("FREE =",free)

    savings = int(total) * 0.30
    print("SAVINGS =",savings)

    business = int(total) * 0.15
    print("BUSINESS =",business)

    investment = int(total) * 0.20
    print("INVESTMENTS =",investment)

    gifts = int(total) * 0.10
    print("GIFTS =",gifts)

calculate = Button(window, text="Calculate", command=calculate)
calculate.pack()


window.mainloop() #places window on computer screen

解决方案

你可以用Tkinter的Label组件替代print来在GUI上展示计算结果,以下是修改后的完整代码,同时优化了界面布局和错误处理:

from tkinter import *

window = Tk()
window.geometry("360x360")
window.title("Gold River 2.0")
window.config(background="#181818")

# 输入周收入
earnings = Entry(window, bg="#333333", fg="white")
earnings.pack(pady=10)

# 创建用于显示结果的标签
result_labels = {
    "total": Label(window, text="", bg="#181818", fg="white"),
    "free": Label(window, text="", bg="#181818", fg="white"),
    "savings": Label(window, text="", bg="#181818", fg="white"),
    "business": Label(window, text="", bg="#181818", fg="white"),
    "investment": Label(window, text="", bg="#181818", fg="white"),
    "gifts": Label(window, text="", bg="#181818", fg="white")
}

# 将标签添加到界面
for label in result_labels.values():
    label.pack(pady=2)

def calculate():
    try:
        total = int(earnings.get())
        # 更新各结果标签的文本内容
        result_labels["total"].config(text=f"${total} earnt")
        
        free = total * 0.25
        result_labels["free"].config(text=f"FREE = ${free:.2f}")
        
        savings = total * 0.30
        result_labels["savings"].config(text=f"SAVINGS = ${savings:.2f}")
        
        business = total * 0.15
        result_labels["business"].config(text=f"BUSINESS = ${business:.2f}")
        
        investment = total * 0.20
        result_labels["investment"].config(text=f"INVESTMENTS = ${investment:.2f}")
        
        gifts = total * 0.10
        result_labels["gifts"].config(text=f"GIFTS = ${gifts:.2f}")
    except ValueError:
        # 处理输入非数字的情况,避免程序崩溃
        result_labels["total"].config(text="请输入有效的数字")
        for key in ["free", "savings", "business", "investment", "gifts"]:
            result_labels[key].config(text="")

# 重命名按钮变量,避免和函数名冲突
calculate_btn = Button(window, text="Calculate", command=calculate, bg="#4CAF50", fg="white")
calculate_btn.pack(pady=10)

window.mainloop()

关键修改说明

  • 新增一组Label组件专门承载计算结果,设置了匹配窗口的背景色和白色文字,提升视觉一致性。
  • 把所有print语句替换为Label.config(text=...),直接更新GUI上的内容,同时将金额格式化为两位小数,更符合金额显示规范。
  • 添加try-except块处理输入非数字的情况,防止程序因无效输入崩溃。
  • 调整组件间距(pady参数)优化界面布局。
  • 重命名按钮变量为calculate_btn,避免和calculate函数名冲突,符合编程规范。

内容的提问来源于stack exchange,提问作者penehama

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 09:39:25