Discussion:
[Tutor] GUI program
Stephanie Quiles
2015-06-30 05:02:23 UTC
Permalink
Hello, i am attempting to create a GUI program using Python 3.4. please see the pasted code below. Why is nothing showing up? i am using Pycharm to run the program to see what it does and it says there are no errors but it does not show me an output. please let me know where i am falling short on this.

Thank you

__author__ = 'stephaniequiles'

""" Write a GUI program that displays your name and address when a button is clicked. when the user clicks the 'show info' button, the
program should display your name and address, as shown in the sketch."""

import tkinter


class AddressGUI:
def __init__(self):
self.main_window = tkinter.Tk()

self.top_frame= tkinter.Frame()
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)

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()

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

allinfo = AddressGUI()

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-30 12:45:57 UTC
Permalink
Post by Stephanie Quiles
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...
Post by Stephanie Quiles
import tkinter
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.
Post by Stephanie Quiles
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.
Post by Stephanie Quiles
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.
Post by Stephanie Quiles
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Continue reading on narkive:
Loading...