0

プログラムに多数のルート (フレーム) があります。OS の終了ボタン (パネルの上部にある x) のコマンドを設定するにはどうすればよいですか? プログラム全体ではなく、1 つのペインだけを閉じることができるようにしたいと考えています。フレーム自体に独自の終了ボタンを作成するコードを見つけましたが、既に持っているすべてのボタンの位置をリセットする必要はありません。

編集:Ubuntuを使用していることを忘れていました

編集2:

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

これは、3 フレームのオブジェクトを初期化する方法です (画像を投稿するのに十分な評判がありません)。私は Toplevel を見て、Bryan Oakleyが次のようなことをすべきだと言っていると思います:

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

ただし、そうすると 4 つ目の (空の) フレームが表示されますが、必要な 3 つのフレームはそのままで、必要なすべてのウィジェットが存在します。

Tk() と Toplevel() の使用の違いと、それぞれの使用方法を説明していただけますか?

Fredrik のソリューションを使用しようとしましたが、次のエラーが表示されました。

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

前もって感謝します!

4

3 に答える 3

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.

于 2013-07-29T20:45:26.397 に答える
0

おそらくあなたの答えが含まれているこの質問にあなたを導くことができます:

Tkinterでウィンドウクローズイベントを処理するにはどうすればよいですか?

于 2013-07-24T01:39:16.930 に答える