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

Tkinter窗口无按钮显示问题:如何修复该异常?

Tkinter空白窗口无按钮问题修复

你的代码存在两个关键问题导致按钮不显示:

  • 定义了创建按钮的bnt方法,但从未调用该方法,按钮并未被创建
  • 按钮的command参数传入了字符串'abc',正确写法应该直接传入函数对象abc

修复后的完整代码如下:

import tkinter as tk


def abc():
    print('get')


class Application(tk.Tk):

    def __init__(self):
        super(self.__class__, self).__init__()
        self.geometry('300x350')
        self.bnt()  # 调用创建按钮的方法

    def bnt(self):
        bnt1 = tk.Button(self, text='j', command=abc)  # 传入函数对象而非字符串
        bnt1.place(x=0, y=0)


if __name__ == '__main__':
    app = Application()
    app.mainloop()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 23:15:45