简体   繁体   English

如何摆脱 Python Tkinter 根窗口?

[英]How do I get rid of Python Tkinter root window?

Do you know a smart way to hide or in any other way get rid of the root window that appears, opened by Tk() ?您知道隐藏或以任何其他方式摆脱由Tk()打开的出现的根窗口的巧妙方法吗? I would like just to use a normal dialog.我只想使用普通对话框。

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?或者有更聪明的解决方案吗?

Probably the vast majority of of tk-based applications place all the components in the default root window.可能绝大多数基于 tk 的应用程序都将所有组件放在默认的根窗口中。 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.选择隐藏默认窗口并创建自己的窗口是一件非常好的事情,尽管它只需要一点点额外的工作。

To answer your specific question about how to hide it, use thewithdraw method of the root window:要回答有关如何隐藏它的具体问题,请使用根窗口的撤销方法:

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:一旦你与对话完成后,你可以与其他所有的Tkinter部件一起销毁根窗口破坏方法:

root.destroy()

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.在纯 Tk 中有一种称为“wm”的方法来管理窗口。 There you can do something like "wm withdraw .mywindow" where '.mywindow' is a toplevel.在那里你可以做一些类似“wm撤回.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()

On OSX, iconify seems to work better:在 OSX 上, iconify 似乎工作得更好:

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

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()

I need to check whether it's withdrawn or not, below is the solution.我需要检查它是否被withdrawn ,下面是解决方案。

import tkinter as tk
root = tk.Tk()
root.withdraw()
print(root.wm_state())
if root.wm_state() == 'withdrawn':  # <----
    root.iconify()
root.mainloop()

withdraw 提取

Removes the window from the screen ( without destroying it ).从屏幕上移除窗口(不破坏它)。 To redraw the window, use deiconify.要重绘窗口,请使用 deiconify。 When the window has been withdrawn, the state method returns "withdrawn" .当窗口被撤回时,状态方法返回"withdrawn"

deiconify 去图标化

redraw the window重新绘制窗口

iconify 图标化

Turns the window into an icon ( without destroying it ).将窗口变成图标(不破坏它)。 To redraw the window, use deiconify.要重绘窗口,请使用 deiconify。 Under Windows, the window will show up in the taskbar .在 Windows 下,该窗口将显示在任务栏中 When the window has been iconified, the state method returns当窗口被图标化后,状态方法返回

state状态

normal, iconify, withdrawn, icon正常, 图标化, 撤回, 图标

This way will work fine:这种方式可以正常工作:

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

Or this one:或者这个:

root = tk.Tk()
root.overrideredirect(1)
root.withdraw()

Two things you must not forget:不可忘记的两件事:

  1. Don forget to import the class:不要忘记导入类:

    import tkinter as tk将 tkinter 作为 tk 导入

  2. Place the above commands in the main windows, outside ANY FUNCTION将上述命令放在主窗口中,在 ANY FUNCTION 之外

For Python 3.0 and higher, to hide the window you need to write the following:对于 Python 3.0 及更高版本,要隐藏窗口,您需要编写以下内容:

import tkinter
tkinter.Tk().withdraw()
root.deiconify()
root.withdraw()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM