[Tutor] Shorten Code

Alan Gauld alan.gauld at btinternet.com
Tue Nov 22 21:43:27 CET 2011

On 22/11/11 19:41, Mic wrote:

>> 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.
> No, I didn't mean that it have to work online. It is just fine if it
> works on the desktop.

OK, So I'll guess you want a desktop program that eventually sends 
commands to an online service?
If so thats a lot easier to do with Tkinter... :-)

> def chair1_clicked(self):
> """This method runs if chair one is clicked"""

First thing to do is take the variable out of the function name and pass 
it as a parameter.

def chair_clicked(self, chairID):

Now you can call that and pass in the actual chair that was
clicked as a parameter. (I'm going to assume you went for a list of 
chairs so the actual value of aChair is the index in the list)

> def change_chair1_value():
> global chair1_value
> button2_value=not chair1_value

You really want to stop using global values.
Make them attributes of your class. Life will be much easier!
And you won;t need all these little functions cluttering up
your code.

> if chair_value:


if self.chairs[chairID][0]

This assumes you store the value and button as a tuple and value
is the first element. So when you create the chairs your code looks like:

for ch in ch_data:
     chairs.append((ch_data[0], Button(self, text=ch_data[1],....) )

> self.chair1.configure(bg="yellow")

You are still hard coding the color rather than using your variables

> text_file=open("Hamburg_Dortmund20_00","w")
> text_file.write("Hamburg-Dortmund")
> text_file.close()

Hmmm, that looks suspicious. You just created a text file with
one line.

> def chair_color_red():
> global chair1_color
> chair1_color=("yellow")
> change_button2_color_red()

The function says red but the code says 'yellow'????

Again take the value out of the function name and make it
a parameter.

def set_chair_color(chairID, col):
      chairs[chairID][1] = col

And now I'm assuming the second value of chairs is the color. Its 
getting complicated. Maybe time we introduced dictionaries(*)....
Then it looks like:

def set_chair_color(chairID, col):
      chairs[chairID]['color'] = col

and your initialisation code looks like:

for id,ch in enumerate(ch_data):
     chairs[id] = {'value':0, 'color':'white',
                   'button' = Button(self, text=ch_data[0],....) )

> self.chair1.configure(bg="green")
> os.remove ("Hamburg_Dortmund20_00")

And now you delete that file you created without having
done anything with it?

I suspect this is all a bit much in one go, if so, we can
break it down into more manageable chunks.

(*)Instead of a dictionary we could have used a class
but I think that's a leap too far for you just yet. But
this is a project that would be easier using classes
and objects. An exercise for the future.

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list