【发布时间】:2015-06-28 18:36:32
【问题描述】:
我刚刚开始使用 Python 的 tkinter GUI 工具。在我的代码中,我创建了一个带有一个按钮的简单 GUI,如果用户单击该按钮,我想向用户显示 messagebox。
目前,我使用tkinter.messagebox.showinfo 方法。我使用 IDLE 在 Windows 7 计算机上编码。如果我从 IDLE 运行代码一切正常,但如果我尝试在 Python 3 解释器中独立运行它,它就不再工作了。相反,它会将此错误记录到控制台:
AttributeError:'module' object has no attribute 'messagebox'
你有什么建议给我吗?我的代码是:
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,然后将tkinter.messagebox.showinfo("blab","bla")替换为messagebox.showinfo("blab","bla"),看看是否有变化。
标签: python user-interface python-3.x tkinter