[Tutor] GUI program

Alan Gauld alan.gauld at yahoo.co.uk
Sat Apr 4 19:50:53 EDT 2020


On 04/04/2020 21:14, Nick wrote:
>    You can try this code:

I'm sure there are some very brave or foolhardy people
who will run random code sent by a complete stranger
on the internet.
I'm not one of them.

Can you tell us what you expect to gain from the post?
Do you have a problem with the code? If so post any error
messages or describe the issue.

Or do you just want a code critique? (see below)
Or somethoing else?


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

>            self.texta = tkinter.StringVar()
>            self.textb = tkinter.StringVar()
>            self.textc = tkinter.StringVar()
>            self.name_label = tkinter.Label(self.top_frame, 
                                             textvariable = self.texta)
>            self.street_label = tkinter.Label(self.top_frame, 
                                               textvariable = self.textb)
>            self.state_label = tkinter.Label(self.top_frame, 
                                              textvariable = self.textc)

>            self.my_button = tkinter.Button(self.bottom_frame, 
                                             text = 'Show Info',
                                             command = self.do_something)
>            self.quit_button = tkinter.Button(self.bottom_frame, 
                                               text = 'Quit',
                                               command =
self.main_window.destroy)

>            # Packing the Labels from top to bottom
>            self.name_label.pack(side = 'top')
>            self.street_label.pack(side = 'top')
>            self.state_label.pack(side = 'top')

>            # Packing the Buttons from left to right
>            self.my_button.pack(side = 'left')
>            self. quit_button.pack(side = 'left')

>            # Packing the frames
>            self.top_frame.pack()
>            self.bottom_frame.pack()

>            # Enter the tkinter main loop
>            tkinter.mainloop()

Shouldn't that be

self.main_window.mainloop()

>    def do_something(self):

This needs to be aligned with the def __init__() line above.


>        # Text Sets update the widgets above
>            self.texta.set(Steven Marcus')
>            self.textb.set('274 Baily Drive')
>            self.textc.set('Waynesville, NC 27999')
> 
>    my_gui = MyGUI()

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