[英]Tkinter widgets not showing
I'm a beginner and just getting into Tkinter basics. 我是一个初学者,刚开始接触Tkinter的基础知识。 I'm following along with a tutorial, but none of my widgets are appearing in the window.
我正在跟一个教程一起学习,但是我的小部件都没有出现在窗口中。 No errors.
没有错误。
import Tkinter
class pinger(Tkinter.Tk):
def __init__(self, parent):
Tkinter.Tk.__init__(self, parent)
self.parent = parent
def initialize(self):
self.grid()
button = Tkinter.Button(self,text="Button")
button.grid(column=1,row=0)
if __name__ == "__main__":
app = pinger(None)
app.title('Server Pinger')
app.mainloop()
The window opens without an issue and no errors are shown. 窗口打开没有问题,并且没有显示错误。 The button widget is nowhere to be found, nor is any other widget I try.
按钮小部件无处可寻,我尝试过的任何其他小部件也无处可寻。
Your issue is that according to the indentation the function - initialize() - is outside the class . 您的问题是,根据缩进,函数-initialize()-在类之外。 Also even if the function was inside the class , you never really call it .
同样,即使函数在类内部,也永远不会真正调用它。
In Python, indentation is really important , it is used for defining blocks . 在Python中,缩进非常重要,它用于定义block。 Also , you should call the initialize() function inside your init () function .
另外,您应该在init ()函数内部调用initialize()函数。 Example -
范例-
import Tkinter
class pinger(Tkinter.Tk):
def __init__(self, parent):
Tkinter.Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
button = Tkinter.Button(self,text="Button")
button.grid(column=1,row=0)
if __name__ == "__main__":
app = pinger(None)
app.title('Server Pinger')
app.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.