(n00b) Tkinter trouble
Jason Swails
jason.swails at gmail.com
Mon Nov 14 02:11:45 EST 2011
On Sun, Nov 13, 2011 at 1:27 PM, Jason Swails <jason.swails at gmail.com>wrote:
> Hello,
>
> I'm trying my hand at creating a Tkinter application, but not having much
> luck. I'm trying to have my top level window be a series of buttons with
> different options on them. Every time a button is pressed, it opens up a
> new window with options. While that new window is open, all of the buttons
> on the original window are disabled. However, I want all of those buttons
> to become active again once I quit my new window. I can't seem to
> reactivate those buttons no matter what I do.
>
> This is the approach I'm trying:
>
> #!/usr/bin/env python
>
> from Tkinter import *
>
> class ActionButton(Button):
> def __init__(self, frame, text):
> self.frame = frame
> Button.__init__(self, master=self.frame, text=text,
> command=self.execute)
> def execute(self):
> window = Toplevel()
> new_button = ActionButton(window, '2nd level button')
> quit_button = Button(window, text='Quit!', command=window.destroy)
> window.buttons = [new_button, quit_button]
> for button in window.buttons: button.pack()
> # Deactivate buttons from containing shell
> for button in self.frame.buttons: button.config(state=DISABLED)
> window.mainloop()
> for button in self.frame.buttons: button.config(state=ACTIVE)
>
>
> top = Tk()
> top_button = ActionButton(top, 'Button on top!')
> top_button.pack()
> quit_button = Button(top, text='Quit', command=top.destroy)
> quit_button.pack()
>
> top.buttons = [top_button, quit_button]
>
> top.mainloop()
>
> I'm really kind of running around in the dark here, so any advice or
> explanation is appreciated.
>
Another approach I think will work, and that I'm going to try, is to
subclass Toplevel and simply assign the higher-level frame/window as an
instance attribute. Then, I can reactivate all of the buttons in the
destroy() method before calling the destroy() method of Toplevel on self.
Something like this:
[untested]
class MyToplevel(Toplevel):
def __init__(self, root, **options):
self.root = root
Toplevel.__init__(self, options)
for button in self.root.buttons: button.config(state=DISABLED)
def destroy(self):
for button in self.root.buttons: button.config(state=ACTIVE)
Toplevel.destroy(self)
This allows me to avoid running "mainloop()" on a non-root Toplevel
instance, but links the re-activation of the buttons with the destruction
of the child window (which was the effect I was going for). I must not
understand what mainloop() does, fully (does it only make sense to run it
on Tkinter.Tk()?)
Thanks!
Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20111114/0a224d1d/attachment-0001.html>
More information about the Python-list
mailing list