[Tutor] GUI program

Alan Gauld alan.gauld at btinternet.com
Tue Jun 30 14:45:57 CEST 2015


On 30/06/15 06:02, Stephanie Quiles wrote:
> Hello, i am attempting to create a GUI program using Python 3.4. please see the pasted code below.
 >  Why is nothing showing up?

Because you have defined the GUI inside a class. But you
never instantiate that class so the code never gets executed.

However, there are several other issue that you should
think about...

> import tkinter
>
> class AddressGUI:
>      def __init__(self):
>          self.main_window = tkinter.Tk()
>
>          self.top_frame= tkinter.Frame()

Your widgets, including Frames should always have
an explicit parent defined. Things can get awfully
confused otherwise and debugging what's happening
is a pain. So use self.main_window as your parent
for these frames.

>          self.mid_frame= tkinter.Frame()
>          self.bottom_frame = tkinter.Frame()
>
>          self.name_label= tkinter.Label(self.top_frame,\
>                                           text='Steven Marcus')
>
>          self.name_label.pack(side='left')
>
>
>          self.value = tkinter.StringVar()
>          self.address_label = tkinter.Label(self.mid_frame,\
>                                             text='274 Baily Drive Waynesville, NC 27999')
>
>          self.address_label.pack(side='left')
>
>          self.show_button = tkinter.Button(self.bottom_frame,\
>                                            text="show info",\
>                                            command=self.showinfo)
>          self.quit_button = tkinter.Button(self.bottom_frame,\
>                                            tect ='Quit',\
>                                            command=self.main_window.destroy)

Personally I'd probably use quit() rather than destroy(). The latter 
leaves the objects in memory, and the evebnt loop running, only removing 
the window widgets. quit() closes the event loop and
shuts things down a bit more thoroughly in my experience.


>          self.show_button.pack(side='left')
>          self.quit_button.pack(side='left')
>
>          self.top_frame.pack()
>          self.mid_frame.pack()
>          self.bottom_frame.pack()
>
>          tkinter.mainloop()

Its usually better to start the mainloop outside the application class.
Not least because it makes reuse easier. But also because it offers 
better control of initialisation/ finalisation actions.

>      def showinfo(self):
>          name = 'Steven Marcus'
>          address = '274 Baily Drive Waynesville, NC 27999'
>          info = name + address
>
>          allinfo = AddressGUI()

And a recursive call to the app inside an event handler is a
recipe for confusion. You will have multiple instances around
in memory with potentially multiple event loops (see note on
destroy) above. Especially since you don;t even store a reference
to the new app instance. The allinfo variable gets deleted when
the method completes.

In general its a good idea to byui8ld your program in small
chunks.  Get a basic GUI with windows to display then add
the widgets one by one and test them as you go. Its much easier
to debug things when you only have a small amount of new code
to look at.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list