[Tutor] Python v3 Tkinter GUI program

Alan Gauld alan.gauld at yahoo.co.uk
Mon Oct 24 18:41:41 EDT 2016


On 24/10/16 22:36, Elliott Andrews wrote:
> Hi there, I am making a program in python based on a Subway operation. All
> descriptions and comments codes are provided in the attached program.

Normally attachments are deleted by the server although yours
seems to have gotten through (to me at least).

Its best to post code in the body of the mail although in
your case that's a lot of code so maybe a pastebin would
be better.

Anyways....

> been using IDLE to run the program and it seems to work the way I want it
> too.

OK, But you should always test GUIs outside of an IDE, after all
thats how they will likely be run in the real world and while
IDEs are great development tools they often distort the environment
in subtle ways that can change how things behave.

> However I have heard from many, that global variables are bad practise in
> any programming language, and so I want to basically replace all my global
> variables with another variable that does the same thing.

You need to understand why they are bad practice. In the real world most
non-trivial programs will have a few global variables (even
if its just an instance of a top level class).

The main problems with globals is that it is very hard to keep track
of what is changing them and when. Also they make your code
almost unusable in any other context since you need to replicate
all the variables.

One trivial way to do it in your case is to put them inside
the GUI class. That's still got a lot of the same problems as
globals but at least you can reuse the class without issues.

[incidentally you don't need to label your global variables
as global outside of the functions where they are used. The
global keyword is a way of indicating which globals are used
by a given function, it is not an indication to the rest
of the program that a variable is global.]

> The problem is, some data is entered and then when the data is submitted,
> the GUI window terminates and automatically re-opens for another data
> entry. I need some sort of variable to store some data while the program
> resetts itself.

The usual solution in GUI programs is not to kill the window
but merely to hide it. You can then change the display values
and reshow it again as needed. That way the "global" variables
are still available.

In Tkinter the hide method is called withdraw(). To redisplay
it use the deiconify() method, and if necessary the lift()
method to raise it to the top of the window stack.

I can't help but mention the way you build your message strings.
You should look at string formatting, it is usually a cleaner
and more efficient solution that using the += operator on
strings. The latter involves an awful lot of copying of
strings and while you only do it about 20 times it would be
better practice to insert the data elements using formatting.

There are a lot of other comments I could make about the code
but for now those pointers should move you forward.

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