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

Tkinter按钮启动时响应不稳定,调整窗口后恢复正常

问题:Tkinter计算器按钮响应不稳定

用Tkinter开发了一个简易计算器,窗口启动后按钮的响应性不稳定,有时点击有效,有时无效。但手动调整或移动窗口后,按钮总能正常工作。

代码结构

from tkinter import *

def button_click(number):
    current = e.get()
    e.delete(0, END)
    e.insert(0, str(current) + str(number))

root = Tk()
root.title("Simple Calculator")

e = Entry(root, width=45, borderwidth=5)
e.grid(row=0, column=0, columnspan=3, padx=10, pady=10)

button_1 = Button(root, text="1", command=lambda: button_click(1))
button_1.grid(row=1, column=0)
button_2 = Button(root, text="2", command=lambda: button_click(2))
button_2.grid(row=1, column=1)

root.mainloop()

已尝试的解决方法

  • 强制更新GUI,无效:
root.update_idletasks()
  • 通过代码调整窗口大小,未解决问题:
root.geometry('1x1')
root.geometry('')
  • 延迟主循环,无效果:
root.after(100, root.mainloop)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 09:24:59