0

I have a number of roots (frames) in my program. How do I set the command for the OS's exit button (the x at the top of the panel)? I want to make it so that I can close just one pane, not the entire program. I found code for making your own exit button on the frame itself, but I don't want to have to reset the position of all the buttons I already have.

edit: I forgot to mention I'm using Ubuntu

edit2:

root3 = Tk()
root = Tk()
root2 = Tk()

This is how I'm initializig the objects for three frames (I don't have enough reputation to post images). I looked at Toplevel and I think Bryan Oakley means I should do something like this:

frame1 = Toplevel()
frame2 = Toplevel()
frame3 = Toplevel()

However, doing so makes a fourth (empty) frame appear, while the three I need are still intact with all the widgets I need present.

Could you explain the difference between using Tk() and Toplevel() and how each is meant to be used?

I attempted to use Fredrik's solution but I received the following error:

Traceback (most recent call last):
  File "GUI_Robot_Control.py", line 823, in <module>
    root.protocol("WM_DELETE_WINDOW", root.destroy())
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1630, in wm_protocol
    'wm', 'protocol', self._w, name, command)
_tkinter.TclError: can't invoke "wm" command:  application has been destroyed

Thanks in advance!

  • 2
    Roots and frames are two very different things in Tkinter. Thus, it is kinda hard to help you without any code. Could you please post your script? –  Jul 23 '13 at 20:24
  • If you have multiple root windows (instances of `Tk`), you're doing something very wrong. Is that what you're doing, or when you say "number of roots" are you talking about instances of `Toplevel`? – Bryan Oakley Jul 24 '13 at 10:49

3 Answers3

1

Not clear if you mean multiple Tk instances or multiple Toplevel windows, but if you need to control what happens when a user clicks the close button on either of them, you can use register a protocol handler:

widget.protocol("WM_DELETE_WINDOW", handler)

After this call, the close button will call the function handler instead of closing the window. The default behaviour is similar to:

widget.protocol("WM_DELETE_WINDOW", widget.destroy)

(yes, this is a bit obscure, but any Tkinter book should explain this)

Fredrik
  • 940
  • 4
  • 10
1

In an edit to the original question you ask about the difference between Tk and Toplevel.

Tk() creates a root window. Your app must always have exactly one of these. All tkinter apps must have a root window, that's just how it's designed to work. When this window is destroyed, your app will exit (typically, though it's technically possible for that not to be true).

Toplevel(...) creates new, "top level" windows. That is, windows that are not directly connected to the root window. These float on the desktop and can have a title bar, close buttons, etc. They look and behave almost exactly like the root window, except that you can destroy instances of Toplevel without destroying your entire application.

If you are creating an app with three windows, you have two choices. First, you can use the root window and two instances of Toplevel. The second option is to create a root window and three instances of Toplevel, then hide the original root window.

In every case, you can use the protocol method to catch when the window is destroyed by the user clicking on a button in the titlebar. You can then either veto the destruction of the window, or do some other things such as closing a database connection, opening a dialog to ask the user to save unsaved data, etc.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks! I was able to fix the issue by having one root window and two instances of 'Toplevel' as you suggested! – Aniruddha N Jul 30 '13 at 21:13
0

May I direct you to this question which probably has your answer in it:

How do I handle the window close event in Tkinter?

Community
  • 1
  • 1
The-IT
  • 660
  • 8
  • 19
  • I tried that but I got the error in the edit above. Thanks though! If you have any other suggestions I'd greatly appreciate them! – Aniruddha N Jul 29 '13 at 20:22