Tkinter 中的顶层:防止打开两个窗口 [英] Toplevel in Tkinter: Prevent Two Windows from Opening

查看:32
本文介绍了Tkinter 中的顶层:防止打开两个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些简单的代码,如下所示:

Say I have some simple code, like this:

from Tkinter import *
root = Tk()
app = Toplevel(root)
app.mainloop()

这会打开两个窗口:Toplevel(root) 窗口和 Tk() 窗口.

This opens two windows: the Toplevel(root) window and the Tk() window.

是否可以避免打开Tk() 窗口(root)?如果是这样,如何?我只想要顶级.我希望发生这种情况,因为我正在制作一个将打开多个窗口的程序,这些窗口都是 rootToplevel.

Is it possible to avoid the Tk() window (root) from opening? If so, how? I only want the toplevel. I want this to happen because I am making a program that will have multiple windows opening, which are all Toplevel's of the root.

谢谢!

推荐答案

withdraw() 方法从屏幕上移除窗口.
iconify() 方法最小化窗口,或者把它变成一个图标.
deiconify() 方法将重绘窗口,和/或激活它.

The withdraw() method removes the window from the screen.
The iconify() method minimizes the window, or turns it into an icon.
The deiconify() method will redraw the window, and/or activate it.

如果您选择 withdraw(),请确保您在测试前考虑了退出程序的新方法.
例如

If you choose withdraw(), make sure you've considered a new way to exit the program before testing.
e.g.

from Tkinter import * # tkinter in Python 3

root = Tk()
root.withdraw()

top = Toplevel(root)
top.protocol("WM_DELETE_WINDOW", root.destroy)

but = Button(top, text='deiconify')
but['command'] = root.deiconify
but.pack()

root.mainloop()

protocol() 方法可以用来注册一个函数,当
顶级窗口的关闭按钮被按下.在这种情况下,我们可以使用 destroy() 退出.

The protocol() method can be used to register a function that will be called when the
Toplevel window's close button is pressed. In this case we can use destroy() to exit.

这篇关于Tkinter 中的顶层:防止打开两个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆