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

Tkinter按钮功能异常:三角形边长计算工具输入标签显示错误

勾股定理三角形未知边长计算应用标签显示问题修复

问题根源

  1. 切换未知边时未隐藏已显示的输入框与标签,导致新旧控件叠加,视觉上出现标签错乱
  2. functiona中错误调用了calculate_button,且按钮文本固定为"side b",不符合功能预期
  3. 同时混用pack()与place()布局,导致控件渲染状态混乱

修复方案

  • 每个功能函数执行时,先隐藏所有输入框和标签
  • 动态设置计算按钮的文本,匹配当前要计算的未知边
  • 移除冗余的pack()调用,仅使用place()进行定位

修复后代码

from tkinter import*
import tkinter as tk
from tkinter import messagebox

Window = Tk()
Window.configure(bg='light blue')
Window.geometry("450x250")

hi = tk.Label(
    text="Hello, this is an app to calculate the unknown side of a triangle.\nusing the pythagoras theorem\nyou know what pythagoras theorem is I presume, if not\ngoogle it",
    fg="black",
    bg="light blue",
    width=75,
    height=4)
hi.pack(side=TOP)

# 初始化控件
labela = tk.Label(text="a", bg='light blue')
labelb = tk.Label(text="b", bg='light blue')
labelc = tk.Label(text="c", bg='light blue')
calculate_button = tk.Button(
    width=10,
    height=2,
    bg="black",
    fg="light blue",)
entrya = tk.Entry()
entryb = tk.Entry()
entryc = tk.Entry()

# 定义隐藏所有输入框和标签的辅助函数
def hide_all():
    entrya.place_forget()
    entryb.place_forget()
    entryc.place_forget()
    labela.place_forget()
    labelb.place_forget()
    labelc.place_forget()
    calculate_button.place_forget()

def functiona():
    hide_all()
    # 设置计算按钮文本为"Calculate a"
    calculate_button.config(text="Calculate a")
    calculate_button.place(relx=.5, rely=.8, anchor="center")
    # 显示已知边b和c的输入框及标签
    entryb.place(relx=.8, rely=.4, anchor="center")
    entryc.place(relx=.8, rely=.6, anchor="center")
    labelb.place(relx=.6, rely=.4, anchor="center")
    labelc.place(relx=.6, rely=.6, anchor="center")

def functionb():
    hide_all()
    calculate_button.config(text="Calculate b")
    calculate_button.place(relx=.5, rely=.8, anchor="center")
    entrya.place(relx=.8, rely=.4, anchor="center")
    entryc.place(relx=.8, rely=.6, anchor="center")
    labela.place(relx=.6, rely=.4, anchor="center")
    labelc.place(relx=.6, rely=.6, anchor="center")

def functionc():
    hide_all()
    calculate_button.config(text="Calculate c")
    calculate_button.place(relx=.5, rely=.8, anchor="center")
    entrya.place(relx=.8, rely=.4, anchor="center")
    entryb.place(relx=.8, rely=.6, anchor="center")
    labela.place(relx=.6, rely=.4, anchor="center")
    labelb.place(relx=.6, rely=.6, anchor="center")

# 未知边选择按钮
button1 = tk.Button(
    command=functiona,
    text="side a",
    width=10,
    height=2,
    bg="black",
    fg="light blue",
)
button1.place(relx=.3, rely=.8, anchor="e")

button2 = tk.Button(
    command=functionb,
    text="side b",
    width=10,
    height=2,
    bg="black",
    fg="light blue",
)
button2.place(relx=.3, rely=.6, anchor="e")

button3 = tk.Button(
    command=functionc,
    text="side c",
    width=10,
    height=2,
    bg="black",
    fg="light blue",
)
button3.place(relx=.3, rely=.4, anchor="e")

Window.mainloop()

内容的提问来源于stack exchange,提问作者dr. moon

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 08:25:17