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

Rick Johnson rantingrickjohnson at gmail.com
Thu Mar 1 22:19:55 EST 2012


On Mar 1, 9:15 pm, John Salerno <johnj... at gmail.com> wrote:
> > 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?

Hmm, it seems as though i am the latest victim of the "copy/paste
error"! Oh well, if you were going to absorb my teachings, you would
have absorbed them by now. I am moving on unless a new subject needs
explaining.



More information about the Python-list mailing list