简体   繁体   中英

Tkinter widgets not showing

I'm a beginner and just getting into Tkinter basics. 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 . 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 . Also , you should call the initialize() function inside your init () function . 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM