如何摆脱Python Tkinter根窗口?

时间:2023-01-24 16:35:14

Do you know a smart way to hide or in any other way get rid of the root window that appears, opened by Tk()? I would like just to use a normal dialog.

你知道一种聪明的方式来隐藏或以任何其他方式摆脱由Tk()打开的根窗口吗?我想只使用一个普通的对话框。

Should I skip the dialog and put all my components in the root window? Is it possible or desirable? Or is there a smarter solution?

我应该跳过对话框并将所有组件放在根窗口中吗?是可能还是可取的?或者是否有更智能的解决方案?

4 个解决方案

#1


52  

Probably the vast majority of of tk-based applications place all the components in the default root window. This is the most convenient way to do it since it already exists. Choosing to hide the default window and create your own is a perfectly fine thing to do, though it requires just a tiny bit of extra work.

可能绝大多数基于tk的应用程序将所有组件放在默认的根窗口中。这是最方便的方法,因为它已经存在。选择隐藏默认窗口并创建自己的窗口是一件非常好的事情,尽管它只需要一点额外的工作。

To answer your specific question about how to hide it, use the withdraw method of the root window:

要回答有关如何隐藏它的特定问题,请使用根窗口的withdraw方法:

import Tkinter as tk
root = tk.Tk()
root.withdraw()

If you want to make the window visible again, call the deiconify (or wm_deiconify) method.

如果要再次显示窗口,请调用deiconify(或wm_deiconify)方法。

root.deiconify()

Once you are done with the dialog, you can destroy the root window along with all other tkinter widgets with the destroy method:

完成对话后,可以使用destroy方法销毁根窗口以及所有其他tkinter小部件:

root.destroy()

#2


11  

I haven't tested since I don't have any Python/TKinter environment, but try this.

我没有测试过,因为我没有任何Python / TKinter环境,但试试这个。

In pure Tk there's a method called "wm" to manage the windows. There you can do something like "wm withdraw .mywindow" where '.mywindow' is a toplevel.

在纯Tk中,有一种称为“wm”的方法来管理窗口。在那里你可以做一些像“wm withdraw .mywindow”这样的地方,其中'.mywindow'是一个*的。

In TkInter you should be able to do something similar to:

在TkInter中你应该可以做类似的事情:

root = Tkinter.Tk()
root.withdraw() # won't need this

If you want to make the window visible again, call the deiconify (or wm_deiconify) method.

如果要再次显示窗口,请调用deiconify(或wm_deiconify)方法。

root.deiconify()

#3


4  

On OSX, iconify seems to work better:

在OSX上,iconify似乎更好用:

root = Tkinter.Tk()
root.iconify()

#4


0  

If you don't want there to a be "flash" as the window is created, use this slight variation:

如果您不希望在创建窗口时出现“闪烁”,请使用以下轻微变化:

import Tkinter as tk
root = tk.Tk()
root.overrideredirect(1)
root.withdraw()

#1


52  

Probably the vast majority of of tk-based applications place all the components in the default root window. This is the most convenient way to do it since it already exists. Choosing to hide the default window and create your own is a perfectly fine thing to do, though it requires just a tiny bit of extra work.

可能绝大多数基于tk的应用程序将所有组件放在默认的根窗口中。这是最方便的方法,因为它已经存在。选择隐藏默认窗口并创建自己的窗口是一件非常好的事情,尽管它只需要一点额外的工作。

To answer your specific question about how to hide it, use the withdraw method of the root window:

要回答有关如何隐藏它的特定问题,请使用根窗口的withdraw方法:

import Tkinter as tk
root = tk.Tk()
root.withdraw()

If you want to make the window visible again, call the deiconify (or wm_deiconify) method.

如果要再次显示窗口,请调用deiconify(或wm_deiconify)方法。

root.deiconify()

Once you are done with the dialog, you can destroy the root window along with all other tkinter widgets with the destroy method:

完成对话后,可以使用destroy方法销毁根窗口以及所有其他tkinter小部件:

root.destroy()

#2


11  

I haven't tested since I don't have any Python/TKinter environment, but try this.

我没有测试过,因为我没有任何Python / TKinter环境,但试试这个。

In pure Tk there's a method called "wm" to manage the windows. There you can do something like "wm withdraw .mywindow" where '.mywindow' is a toplevel.

在纯Tk中,有一种称为“wm”的方法来管理窗口。在那里你可以做一些像“wm withdraw .mywindow”这样的地方,其中'.mywindow'是一个*的。

In TkInter you should be able to do something similar to:

在TkInter中你应该可以做类似的事情:

root = Tkinter.Tk()
root.withdraw() # won't need this

If you want to make the window visible again, call the deiconify (or wm_deiconify) method.

如果要再次显示窗口,请调用deiconify(或wm_deiconify)方法。

root.deiconify()

#3


4  

On OSX, iconify seems to work better:

在OSX上,iconify似乎更好用:

root = Tkinter.Tk()
root.iconify()

#4


0  

If you don't want there to a be "flash" as the window is created, use this slight variation:

如果您不希望在创建窗口时出现“闪烁”,请使用以下轻微变化:

import Tkinter as tk
root = tk.Tk()
root.overrideredirect(1)
root.withdraw()