简体   繁体   English

使用tkMessageBox而不导入Tkinter

[英]Using tkMessageBox without importing Tkinter

I'm writing a module where I want to support popup dialogs to indicate errors, but I don't need or want a root window (because I want the module to be independent of the main GUI and share-able between multiple calling applications). 我正在编写一个模块,在其中我要支持弹出对话框来指示错误,但是我不需要或想要一个根窗口(因为我希望该模块独立于主GUI并在多个调用应用程序之间可共享) 。 I tried simply doing this: 我只是尝试这样做:

import tkMessageBox
[...stuff...]
if (errorCondition): tkMessageBox.showwarning("My Module","That won't work!")

...but when I run it, a root window appears alongside the message box. ...但是当我运行它时,在消息框旁边会出现一个根窗口。 I know about the withdraw() method, but since I never imported Tkinter itself and never instantiated Tkinter.Tk() , there's no object for me to use withdraw() on. 我知道withdraw()方法,但是因为我从未导入Tkinter本身,也从未实例化Tkinter.Tk() ,所以没有对象可以使用withdraw()

An alternative that works is to go ahead and import Tkinter anyway, so I can create the root window myself and then withdraw() it: 一个Tkinter的替代方法是继续导入Tkinter ,因此我可以自己创建根窗口,然后对它进行withdraw()

import Tkinter
import tkMessageBox
root = Tkinter.Tk()
root.withdraw()
[...stuff...]
if (errorCondition): tkMessageBox.showwarning("My Module","That won't work!")

...but even though that works, it seems klunky to bring in a module and instantiate an object just so I can get rid of it. ...但是即使那行得通,但引入一个模块并实例化一个对象似乎很笨拙,以至于我可以摆脱它。 Plus I don't want to confuse things between this root, and the "real" root in the calling applications. 另外,我不想混淆此根与调用应用程序中的“真实”根之间的关系。

Going back to the first example, it's obvious that tkMessageBox is doing something under the hood to create the root window on its own. 回到第一个示例,很明显tkMessageBox正在做一些事情来自行创建根窗口。 Is there any way I can grab a reference to that root window so I can withdraw() it? 有什么办法可以获取对该根窗口的引用,以便可以对其进行withdraw()

(Environment is Windows 7 and Python 2.7.3.) (环境为Windows 7和Python 2.7.3。)

tkMessageBox is built on top of Tkinter, so it is impossible to simply get rid of it: All the functions of the module, like tkMessageBox.showwarning , are wrappers of the _show function. tkMessageBox建立在Tkinter之上,因此不可能简单地摆脱它:该模块的所有功能,例如tkMessageBox.showwarning ,都是_show函数的包装。 This function creates a Message object, with different arguments depending of the type of dialog you use. 此函数创建一个Message对象,根据您使用的对话框类型,使用不同的参数。 Message is a subclass of Dialog , which in turn is a subclass of Toplevel . MessageDialog的子类,而Dialog又是Toplevel的子类。

Toplevel is a Tkinter widget, so the very first line of this module (apart from the comments) where Dialog is defined is: Toplevel是Tkinter小部件,因此定义Dialog的该模块的第一行(除了注释)是:

from Tkinter import *

Your second solution is the only way to use tkMessageBox correctly, since you are forced to use (at least internally) Tkinter with it. 第二种解决方案是正确使用tkMessageBox的唯一方法,因为您被迫(至少在内部)使用Tkinter。

References: 参考文献:

Note : tkMessageBox does not use tkSimpleDialog, it uses tkCommonDialog. 注意 :tkMessageBox不使用tkSimpleDialog,它使用tkCommonDialog。 The code of tkCommonDialog is here tkCommonDialog的代码在这里

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

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