Tkinter Dialog Management problems:

Michael Yanowitz m.yanowitz at kearfott.com
Thu May 18 11:52:54 EDT 2006


> Hello:
>
>    Below I have included a stripped down version of the GUI I am working
> on.
> It contains 2 dialog boxes - one main and one settings. It has the
> following
> problems, probably all related, that I am hoping someone knows what I am
> doing wrong:
>
> 1) Pressing the Settings.. Button multiple times, brings up many
> instances
>    of the Settings Panel. I just want it to bring up one. Is there an
> easy
>    way to do that?

In fact, the two windows you created are not dialogs; they're just
windows. To turn a window into an actual "dialog", i.e basically to make
it modal, you have to do the following operations (supposing your dialog
window is named dlg and your main window in named root):

## Ensure only window can receive user events
dlg.grab_set()
## Force Dialog to stay on top of main window
dlg.transient(root)
## Wait for dialog to be destroyed
root.wait_window(dlg)

> 2) Pressing the Done button in the Settings Panel, just erases the Done
> button
>    (and any other widgets in the Panel). It does not dismiss the Panel.
> Pressing
>    the X button does work. What callback is that? Can I make the Done
> button
> call
>    that instead? How?

This is not the way it works. In fact, what you did wrong is something
that has been around for years in some Tkinter tutorial(s): you made your
classes inherit from Frame. This is a Bad Idea: a Frame is not a window,
but only a generic container. There are 2 classes for windows: Tk for the
main window and Toplevel for all others. They both also act as containers,
so you can do in them everything you do in Frames. So make your
ScriptDialog inherit from Tk, your SettingsDialog inherit from Toplevel,
remove all explicit creations of Tkinter.Tk or Tkinter.Toplevel and
instantiate your classes instead. Then calling destroy on either on the
dialogs will actually close the window.

> 3) Pressing the Done button from the Main Panel has no effect? Why not?
> It
> used
>    to work (self.quit()). Again, I would like to call whatever is called
> when the
>    X button (top Right corner) is pressed.

This should work. BTW, your "done" method is not needed: creating the
Button with command=self.quit works without problem.


Thanks.  That helped alot.
However it leaves a couple very minor problems which I think I can live
with.
1) It brings up an empty additional 'main window'.
   I have tried using the Tkinter.NoDefaultRoot() option, but run into
   other problems with other things not defined.
NameError: global name '_default_root' is not defined
Exception exceptions.AttributeError: "IntVar instance has no attribute
'_tk'" in
 <bound method IntVar.__del__ of <Tkinter.IntVar instance at 0x009C7990>>
ignored

2) By deriving the 'dialog' from Tk, existing calls to self.pack() no
   longer are valid, but they don't appear to be necessary.

  My only 'Tkinter tutorial' is what is included in Orielly's "Programming
Python". Still looking for a good tutorial. I am not clear what the
difference
between Tk() and Toplevel() are. They seem totally interchangeable.





More information about the Python-list mailing list