Python Tkinter 8.5导入消息框 [英] Python tkinter 8.5 import messagebox

查看:121
本文介绍了Python Tkinter 8.5导入消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在IDLE内运行良好,否则我将收到"NameError:未定义全局名称'messagebox'".但是,如果我明确声明from tkinter import messagebox,则无论从哪里运行都很好.

The following code runs fine within IDLE, but otherwise I get "NameError: global name 'messagebox' is not defined". However, if I explicitly state from tkinter import messagebox, it runs fine from where ever.

from tkinter import *
from tkinter import ttk 

root = Tk()
mainFrame = ttk.Frame(root)
messagebox.showinfo("My title", "My message", icon="warning", parent=mainFrame)

为什么IDLE不需要显式import语句,而在其他地方则需要它?

Why does IDLE not need the explicit import statement but elsewhere it is required?

推荐答案

消息框是tkinter的一个单独的子模块,因此只需从tkinter完全导入即可:

the messagebox is a separate submodule of tkinter, so simply doing a complete import from tkinter:

from tkinter import *

不导入消息框

它必须像这样被显式导入:

it has to be explicitly imported like so:

from tkinter import messagebox

与必须显式导入ttk相同的方式

in the same way that ttk has to be imported explicitly

它在空闲状态下工作的原因是因为空闲状态导入消息框是出于其自身的目的,并且由于空闲状态的工作方式,它的导入在空闲状态下可以访问

the reason it works in idle is because idle imports messagebox for its own purposes, and because of the way idle works, its imports are accessible while working in idle

这篇关于Python Tkinter 8.5导入消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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