[Tutor] Shortening code.

Alan Gauld alan.gauld at btinternet.com
Tue Nov 22 20:00:09 CET 2011


On 22/11/11 15:11, Mic wrote:

> > Also, do you really need the colors, you don't actually use them for
> > anything below except the initial color, but you might as well just hard

> Button1 is supposed to represent chair one in the train.

So call it chair_1 or put it in a list called chairs
so that you can access them as chairs[0] or whatever.
But get a name that reflects what its actually used for.

 > When you click at the button, which represents a chair,
> it is supposed to change color to show that the chair now is booked. If
> you press it one more time, it is supposed to
> change color to show that the chair is free to book.

Thats all fine but you are not doing that, so the variable
is currently pointless. Either change the code to use the variable or 
delete the variable which is simply wasting space and time and making 
the code less readable.

> ---------------------------------------------------------------------
> I understand why you think I don't need to change the global value,
> since I am already changing the color with this line.
> self.hello_bttn1.configure(bg="green", text="Hi_1")
>
> The thing is that this window I have created is just a window in another
> window. I want the color to be the same that it was before I closed the
> window.
> Otherwise the color would be green when I open the window again, despite
> closing the window when the button was red. I hope you understand what I
> mean by this?

Yes, but the point is that you are not using the variable just now. So 
use it even if its not part of your master plan just yet. Or, if the 
code is only to demonstrate a point leave it out completely


> at the top of my email, I am writing a larger program. This program is
 > supposed to be a GUI  online booking program for train tickets.

OK, That's a pity.
A Tkinter program is never going to be an online GUI, it only works on 
the desktop. For that you need to use HTML5/Flash/Javascript/Java etc.
And its all a lot more messy.

> However, I understand if you don't have time to answer these probably
> stupid and simple questions, I am still  grateful for your previous answers!

They are neither simple nor stupid, they are very sensible questions and 
you are tackling a significant program. Sadly I don't think your 
approach will work for an 0nline solution (ie a web based one).

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list