On 26/11/11 16:16, Mic wrote:

I will first post the entire program here, (not a lot of code)
> and then ask the question.

<---snipped --->

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!

That's right, because your SeatButton init doesn't check to see if a file exists for it, it always colors it green. You need to read the directory and see whether a file for that seat exists. If it does
color it red. (Hint: look at os.path.exists() )


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!

This is one case where I think it will be easier to show you than tell you so I've pasted my modified version of your code. Note that I've moved all the clsses to the outer level and made the second window(which I've renamed SeatWindow) inherit from Toplevel rather than Frame so that you can create it as a child of the Mainwindow. That then allows a single mainloop. It will be worth while studying the differences to see if you understand why I've done what I've done.

There is still quite a lot could be done to tidy it up but let's get the buttons working right first....


###################################

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):
        SeatWindow(root)

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

    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 SeatWindow(tk.Toplevel):
     def __init__(self, master):
         super (SeatWindow, 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()
root.title("testV2")
app=Mainwindow(root)
root.mainloop()


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

_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to