Is it necessary to call Tk() when writing a GUI app with Tkinter?

John Salerno johnjsal at gmail.com
Thu Mar 1 22:15:21 EST 2012


> EXAMPLE 1: (this works, but is flawed!)
>  root = tk.Tk()
>  b = tk.Button(master=None, text='Sloppy Coder')
>  b.pack()
>  root.mainloop()
> 
> EXAMPLE 2: (This is how to write code!)
>  root = tk.Tk()
>  widgetframe = tk.Frame(root)
>  b = tk.Button(master=None, text='Sloppy Coder')
>  b.pack()
>  root.mainloop()
> 
> EXAMPLE 3: (OOP style)
>  class App(tk.Tk):
>      def __init__(self):
>          tk.Tk.__init__(self)
>          # something should happen here to justify using OOP
>      # or here
> 
>  class AppFrame(tk.Frame):
>      def __init__(self, master, **kw):
>          tk.Frame.__init__(self, master, **kw)
>          self.createWidgets()
> 
>      def createWidgets(self):
>          b = tk.Button(master=None, text='Push Me')
>          b.pack()
> 
>  if __name__ == '__main__':
>      app = App()
>      frame = AppFrame(app)
>      frame.pack()
>      app.mainloop()

Why is the master argument for Button set to None? Shouldn't it be the Frame object? And shouldn't it also have self as the first argument?



More information about the Python-list mailing list