[Tutor] Shorten this code

Peter Otten __peter__ at web.de
Sun Nov 27 13:53:09 CET 2011


Mic wrote:

> class SeatButton(tk.Button):
>      def __init__(self, master, index):
>          text = str(index+1)
>          super(SeatButton, self).__init__(master,
>                                           text=text, bg=FREE,
>                                           command=self.clicked)
>          self.filename = "Germany_France{}.txt".format(index+1)
>          self.occupied = False
>          if os.path.exists(self.filename):
>               self["bg"]=OCCUPIED

In the above snippet button color and the occupied-flag can get out of sync. 
If a seat is occupied the color is correcly updated to red bot the 
self.occupied remains False.
A simple fix:

self.occupied = os.path.exists(self.filename)
if self.occupied:
    self["bg"] = OCCUPIED 




More information about the Tutor mailing list