简体   繁体   English

Tkinter MessageBox 给出错误

[英]Tkinter MessageBox gives error

In a GUI program written in Python 3.5 I use the Tkinter module.在用 Python 3.5 编写的 GUI 程序中,我使用了 Tkinter 模块。 I define a function to call the MessageBox as follows:我定义了一个函数来调用 MessageBox,如下所示:

def callAbout():
    messagebox.showinfo(title = "About", message = "My Window")

When I try to execute, I get the following error message:当我尝试执行时,我收到以下错误消息:

Exception in Tkinter callback
Traceback (most recent call last):
  [PATH]
    return self.func(*args)
  File "tk-error.py", line 4, in callAbout
    messagebox.showinfo(title = "About", message = "My Window")
NameError: name 'messagebox' is not defined

It seems that the program cannot find程序好像找不到

messagebox

but I wonder why, since I imported the whole module with但我想知道为什么,因为我导入了整个模块

from tkinter import *

Please, ask if you need the complete code.请问您是否需要完整的代码。 Thanks in advance for your help.在此先感谢您的帮助。

Added: Here follows the whole code.补充:这里是整个代码。

from tkinter import *

def callAbout():
    messagebox.showinfo(title = "About", message = "My Window")

win = Tk()

win.geometry('300x300')
win.title("My First Window")

bar_menu = Menu(win)

menu_about = Menu(bar_menu, tearoff = 0)
bar_menu.add_cascade(label = "About", menu = menu_about)
menu_about.add_command(label = "About", command = callAbout)

win.config(menu = bar_menu)

win.mainloop()

I had same problem.我有同样的问题。 I changed the code like this.我像这样更改了代码。 simply...简单地...


from tkinter import *

from tkinter import messagebox

Change messagebox.showinfo to showinfo and add from tkinter.messagebox import showinfomessagebox.showinfo更改为showinfo并添加from tkinter.messagebox import showinfo

from tkinter import *
from tkinter.messagebox import showinfo


def callAbout():
    showinfo(title="About", message="My Window")


win = Tk()

win.geometry('300x300')
win.title("My First Window")

bar_menu = Menu(win)

menu_about = Menu(bar_menu, tearoff=0)
bar_menu.add_cascade(label="About", menu=menu_about)
menu_about.add_command(label="About", command=callAbout)

win.config(menu=bar_menu)

win.mainloop()

Output:输出:

在此处输入图片说明

You can import and use the messagebox module by using an alias:您可以使用别名导入和使用messagebox模块:

import tkinter.messagebox as tkmb

Here is your code after making changes,这是更改后的代码,

import tkinter as tk
import tkinter.messagebox as tkmb

def callAbout():
    tkmb.showinfo(title = "About", message = "My Window")

win = tk.Tk()

win.geometry('300x300')
win.title("My First Window")

bar_menu = tk.Menu(win)

menu_about = tk.Menu(bar_menu, tearoff = 0)
bar_menu.add_cascade(label = "About", menu = menu_about)
menu_about.add_command(label = "About", command = callAbout)

win.config(menu = bar_menu)

win.mainloop()

messagebox, along with some other modules like filedialog, does not automatically get imported when you import tkinter.导入 tkinter 时,messagebox 以及其他一些模块(如 filedialog)不会自动导入。 Import it explicitly, using as and/or from as desired.根据需要使用 as 和/或 from 显式导入它。 check below 3 examples for better clarification-检查以下 3 个示例以获得更好的说明-

    >>> import tkinter
    >>> tkinter.messagebox.showinfo(message='hi')

Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'messagebox'

. .

    >>> import tkinter.messagebox
    >>> tkinter.messagebox.showinfo(message='hi')

'ok'

. .

 >>> from tkinter import messagebox
    >>> messagebox.showinfo(message='hi')

'ok'

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

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