tkinter.messagebox.showinfo 并不总是有效 [英] tkinter.messagebox.showinfo doesn't always work

查看:54
本文介绍了tkinter.messagebox.showinfo 并不总是有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 Python 的 tkinter GUI 工具.在我的代码中,我创建了一个带有一个按钮的简单 GUI,如果用户单击该按钮,我想向用户显示一个 messagebox.

目前,我使用 tkinter.messagebox.showinfo 方法.我使用 IDLE 在 Windows 7 计算机上编码.如果我从 IDLE 运行代码一切正常,但是如果我尝试在 Python 3 解释器中独立运行它,它就不再工作了.相反,它将此错误记录到控制台:

AttributeError:'module' 对象没有属性 'messagebox'

你对我有什么建议吗?我的代码是:

导入tkinter类 simpleapp_tk(tkinter.Tk):def __init__(self,parent):tkinter.Tk.__init__(self,parent)self.parent = 父母self.temp = 假自我初始化()定义初始化(自我):self.geometry()self.geometry("500x250")self.bt = tkinter.Button(self,text="Bla",command=self.click)self.bt.place(x=5,y=5)定义点击(自我):tkinter.messagebox.showinfo("blab","bla")如果 __name__ == "__main__":app = simpleapp_tk(无)app.title('我的应用程序')app.mainloop()

解决方案

messagebox 以及其他一些模块,例如 filedialog,在您 导入 tkinter.根据需要使用 as 和/或 from 显式导入它.

<预><代码>>>>导入 tkinter>>>tkinter.messagebox.showinfo(message='hi')回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中AttributeError: 'module' 对象没有属性 'messagebox'>>>导入 tkinter.messagebox>>>tkinter.messagebox.showinfo(message='hi')'好的'>>>从 tkinter 导入消息框>>>messagebox.showinfo(message='hi')'好的'

I have just started working with Python's tkinter GUI tool. In my code I create an simple GUI with one button and I want to show the user a messagebox if they click on the button.

Currently, I use the tkinter.messagebox.showinfo method for it. I code on a Windows 7 computer using IDLE. If I run the code from IDLE everything works fine, but if I try to run it standalone in the Python 3 interpreter it doesn't work any more. Instead it logs this error to the console:

AttributeError:'module' object has no attribute 'messagebox'

Do you have any tips for me? My code is:

import tkinter

class simpleapp_tk(tkinter.Tk):
    def __init__(self,parent):
        tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.temp = False
        self.initialize()

    def initialize(self):
        self.geometry()
        self.geometry("500x250")
        self.bt = tkinter.Button(self,text="Bla",command=self.click)
        self.bt.place(x=5,y=5)
    def click(self):
        tkinter.messagebox.showinfo("blab","bla")

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('my application')
    app.mainloop()

解决方案

messagebox, along with some other modules like filedialog, does not automatically get imported when you import tkinter. Import it explicitly, using as and/or from as desired.

>>> import tkinter
>>> tkinter.messagebox.showinfo(message='hi')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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'

这篇关于tkinter.messagebox.showinfo 并不总是有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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