Tkinter Toplevel spawned in event handler

Alexander Slusarczyk aslusarc at pc46.ics.ele.tue.nl
Fri Apr 27 09:11:34 EDT 2001


Hi!

I am a Tkinter rookie, so please bear with me if I'm doing
something obviously stupid.
I am trying to write a simple Tkinter application, which will 
show modeless windows with some content as the reaction to button press.
I am trying to achieve it with the following code:

----

class MyWin(Toplevel):
    def __init__(self,master,number):
        Toplevel.__init__(self,master)

        self.number = number
        Label( self , text=str(self.number) ).pack()

        self.bind("<Escape>", self.cancel)

        self.wait_window(self)

    def cancel(self,event=None):
        print "Canceling",self.number
        self.destroy()
        

def new_window():
    global win_number, root
    no = win_number
    win_number = win_number + 1
    print "Showing",no
    t = MyWin(root,no)
    print "Canceled",no

root=Tk()
win_number=0
Button(root, text="New Window",command=new_window).pack()
root.mainloop()

----

when I run it and create 3 windows (0,1 and 2), and then "<Escape>" them
in the order of their creation I get the following:

Showing 0
Showing 1
Showing 2
Canceling 0
Canceling 1
Canceling 2
Canceled 2
Canceled 1
Canceled 0

it seems that MyWin(root,x) for a window only returns, after the MyWin() for
all the windows created AFTER x have returned (?!)

Am I making any sense? 
Does anyone have an explanation, or - preferably - a fix ?

Thanks in advance,
Alex



More information about the Python-list mailing list