Tkinter (OOP?) help

Discussion in 'Python' started by 3c273, Dec 30, 2004.

  1. 3c273

    3c273 Guest

    Hello,
    I am trying to expand on a tutorial I am working through and I am not
    understanding something. In the following example if you click on the "New
    window" button, a new window appears with a "Close me" button in it and if
    you click on it's "Close me" button, it works. But, if you click the
    original "New window" button two or more times, the "Close me" button only
    works on one of the new windows. Furthermore, if you click "Close me" on the
    first new window, it closes the last window to open, not the one that you
    clicked on. How do I bind a "closeme2" function to each new window? Any help
    is appreciated.

    Louis

    --begin code--
    from Tkinter import *

    class MyApp:
    def __init__(self, parent):
    self.myparent = parent
    self.frame1 = Frame(parent)
    self.frame1.pack()

    self.btn1 = Button(self.frame1, text='Do nothing', bg='green')
    self.btn1.pack(side=LEFT)

    self.btn2 = Button(self.frame1, text='Close me', fg='white',
    bg='red')
    self.btn2.pack(side=LEFT)
    self.btn2.bind('<Button-1>', self.closeme)

    self.btn3 = Button(self.frame1, text='New window?', bg='grey')
    self.btn3.pack(side=LEFT)
    self.btn3.bind('<Button-1>', self.newwindow)

    def closeme(self, event):
    self.myparent.destroy()

    def newwindow(self, event):
    self.new = Toplevel(self.myparent)
    self.frame2 = Frame(self.new)
    self.frame2.pack()

    self.btn4 = Button(self.frame2, text='Close me', fg='white',
    bg='red')
    self.btn4.pack()
    self.btn4.bind('<Button-1>', self.closeme2)

    def closeme2(self, event):
    self.new.destroy()

    root = Tk()
    root.title('Tk Test Program')
    myapp = MyApp(root)
    root.mainloop()
    3c273, Dec 30, 2004
    #1
    1. Advertising

  2. 3c273

    Mike Meyer Guest

    "3c273" <> writes:

    > Hello,
    > I am trying to expand on a tutorial I am working through and I am not
    > understanding something. In the following example if you click on the "New
    > window" button, a new window appears with a "Close me" button in it and if
    > you click on it's "Close me" button, it works. But, if you click the
    > original "New window" button two or more times, the "Close me" button only
    > works on one of the new windows. Furthermore, if you click "Close me" on the
    > first new window, it closes the last window to open, not the one that you
    > clicked on. How do I bind a "closeme2" function to each new window? Any help
    > is appreciated.
    >
    > Louis
    >
    > --begin code--
    > from Tkinter import *
    >
    > class MyApp:
    > def __init__(self, parent):
    > self.myparent = parent
    > self.frame1 = Frame(parent)
    > self.frame1.pack()
    >
    > self.btn1 = Button(self.frame1, text='Do nothing', bg='green')
    > self.btn1.pack(side=LEFT)
    >
    > self.btn2 = Button(self.frame1, text='Close me', fg='white',
    > bg='red')
    > self.btn2.pack(side=LEFT)
    > self.btn2.bind('<Button-1>', self.closeme)
    >
    > self.btn3 = Button(self.frame1, text='New window?', bg='grey')
    > self.btn3.pack(side=LEFT)
    > self.btn3.bind('<Button-1>', self.newwindow)
    >
    > def closeme(self, event):
    > self.myparent.destroy()
    >
    > def newwindow(self, event):
    > self.new = Toplevel(self.myparent)
    > self.frame2 = Frame(self.new)
    > self.frame2.pack()
    >
    > self.btn4 = Button(self.frame2, text='Close me', fg='white',
    > bg='red')
    > self.btn4.pack()
    > self.btn4.bind('<Button-1>', self.closeme2)
    >
    > def closeme2(self, event):
    > self.new.destroy()


    Every time you call newwindow, you rebind self.new to the window just
    created. So any close button that works will close the last window opened.

    You need to create a separate class for new windows, each with it's
    own self.new (or self.window) that it's self.closeme will call destroy
    on.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Dec 31, 2004
    #2
    1. Advertising

  3. 3c273

    3c273 Guest

    "Mike Meyer" <> wrote in message
    news:...

    > Every time you call newwindow, you rebind self.new to the window just
    > created. So any close button that works will close the last window opened.
    >
    > You need to create a separate class for new windows, each with it's
    > own self.new (or self.window) that it's self.closeme will call destroy
    > on.
    >
    > <mike

    Thanks for the pointer.
    Louis
    3c273, Jan 3, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Big Dave
    Replies:
    4
    Views:
    690
    CJ Taylor
    Sep 28, 2004
  2. Learner
    Replies:
    1
    Views:
    350
    Kevin Spencer
    Jan 3, 2006
  3. Jeff Epler
    Replies:
    0
    Views:
    497
    Jeff Epler
    Aug 20, 2004
  4. Jeff Epler
    Replies:
    0
    Views:
    444
    Jeff Epler
    Aug 23, 2004
  5. Ronny Mandal

    OOP and Tkinter

    Ronny Mandal, May 15, 2006, in forum: Python
    Replies:
    4
    Views:
    383
    Diez B. Roggisch
    May 15, 2006
Loading...

Share This Page