[Tutor] Shortening the code

Mic o0MB0o at hotmail.se
Sat Nov 26 17:16:28 CET 2011


>IDLE sometimes slows things down and can occasionally lock 
>up with Tkinter (probably because it is itself written in Tkinter!). 
>It's usually better and faster  to run Tkinter programs from a separate 
>console window.


Okay, as a console window I assume the CMD terminal is OK?


 
>What you are doing is fine, I'd leave the button click bit as it is if you really want to 
>create/delete files. As I said originally this is an unusual thing to do but you may 
>have more going on behind the scenes or planned.

Yes, I actually have one more thing going on before I feel it is time to wrap the program up.
I will first post the entire program here, (not a lot of code) and then ask the question.


from tkinter import*
import tkinter as tk
import os

FREE = "green"
OCCUPIED = "red"

class Mainwindow(Frame):
    def __init__(self,master):
        super(Mainwindow,self).__init__(master)
        self.grid()
        self.create_mainwidgets()

    def create_mainwidgets(self):
        self.klicka=Button(self, text="Press the button", command=self.uppdatera)
        self.klicka.grid()

    def uppdatera(self):
        class SeatButton(tk.Button):
            def __init__(self, master, index):
                text = "{}".format(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
        
        

            def clicked(self):
                self.occupied = not self.occupied
                if self.occupied:
                    self["bg"] = OCCUPIED
                    text_file=open(self.filename,"w")
                    text_file.write(self.filename)
                    text_file.close
                else:
                    self["bg"] = FREE
                    os.remove(self.filename)

                
            

        class Window(tk.Frame):
            def __init__(self, master):
                super (Window, self).__init__(master)
                self.grid()
                self.create_widgets()

            def create_widgets(self):
                for index in range(20):
                    button = SeatButton(self, index)
                    row, column = divmod(index, 4)
                    button.grid(row=row, column=column)

        root = tk.Tk()
        root.title("Test")
        app = Window(root)
        root.mainloop()

root=Tk()
root.title("testV2")
app=Mainwindow(root)
root.mainloop()
        



If you press the button in the first window, a new window open with all the “seats”.
Say that you press one of the buttons so that it turns red. Then you exit this window
and are back in the first one. If you press the button in the window, the window with the “seats” open.

However, the buttons that were red when I exited the window is green! How can I make the be the 
same color they were when I closed the window with the “seats”?



Also, I am aware of the fact that I shouldn’t be using more than one tk() object, I am working on correcting that right now!


Thanks!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111126/48f90cab/attachment-0001.html>


More information about the Tutor mailing list