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
相关产品推荐
相关产品推荐

