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

Rick Johnson rantingrickjohnson at gmail.com
Thu Mar 1 22:02:37 EST 2012


On Mar 1, 8:49 pm, Rick Johnson <rantingrickjohn... at gmail.com> wrote:
> On Feb 29, 11:24 pm, Terry Reedy <tjre... at udel.edu> wrote:
>
> > On 2/29/2012 10:22 PM, Rick Johnson wrote:
> > > PS: I would highly suggest against using the "from Tkinter import *".
> > > Instead, use "import Tkinter as tk" and prefix all module contents
> > > with "tk.".
>
> > I have changed the example to do that. I also showed the alternate to
> > initialize a widget. Here is the current version, tested on Windows 3.2.2.
>
> > import tkinter as tk
>
> > class Application(tk.Frame):
> >      def __init__(self, master=None):
> >          tk.Frame.__init__(self, master)
> >          self.pack()
>
> With all due respect, I would also recommend against "self packing" a
> widget. And i can speak from experience on this issue. There was a
> time when i was self-packing lots of custom compund widgets; then i
> realized later the shortcomings of such action; what if you need to
> use the grid or place geometry mangers instead? So remove the
> self.pack line and add a line to the bottom:
>
> > root = tk.Tk()
> > app = Application(master=root)
> > app.pack() # <-- added this line
> > app.mainloop()
> > There is a minor problem left. The hi_there Button text has underscores
> > because if I use spaces instead, tk surrounds the text with {bra ces}.
> > This seems bizarre. Is there any way to have Button text with spaces and
> > no braces?
>
> Not sure what is happening on your end, but i don't see any braces. In
> any event, here is a slightly modified version of your code that
> follows PEP8 and removes some inconsistencies.
>
> ## START CODE ##
> # Python < 3.0
> import Tkinter as tk
> from Tkconstants import TOP, BOTTOM
> from tkMessageBox import showinfo
>
> class Application(tk.Frame):
>      def __init__(self, master=None):
>          tk.Frame.__init__(self, master)
>          self.createWidgets()
>
>      def createWidgets(self):
>          self.hi_there = tk.Button(self)
>          self.hi_there["text"] = "Hello_World\n(click_me)"
>          self.hi_there["command"] = self.say_hi
>          self.hi_there.pack() # !!!
>          self.qbutton = tk.Button(self, text="Close Application",
>                                   fg="red", command=root.destroy)
>          self.qbutton.pack() # !!!
>
>      def say_hi(self):
>          showinfo('Modal Dialog', "hi there, everyone!", parent=self)
>          print("hi there, everyone!")
>
> if __name__ == '__main__':
>     root = tk.Tk()
>     app = Application(master=root)
>     app.pack()
>     app.mainloop()
> ## END CODE ##

Opps, i just realized that "App" is actually a Tkinter.Frame and not a
Tkinter.Toplevel. So the line:

 showinfo('Modal Dialog', "hi there, everyone!", parent=self)

...is broken. Since we don't have a reference to the root window from
inside the scope of this Frame class, we can use
"self.winfo_toplevel()" to fetch a reference.

 showinfo('Modal Dialog', "hi there, everyone!",
parent=self.winfo_toplevel())

...ahhh! That should work perfectly now!



More information about the Python-list mailing list