Tkinter.messagebox.showinfo doesn't always work
I just got started with Python tkinter
GUI tool . In my code, I am creating a simple one-button GUI and I want to show the user messagebox
if they click on the button.
I am currently using the method for it tkinter.messagebox.showinfo
. I am 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 offline in the Python 3 interpreter it no longer works. Instead, it logs this error to the console:
AttributeError:'module' object has no attribute 'messagebox'
Do you have any tips for me? My code:
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()
source to share
messagebox
, along with some other modules such as filedialog
, is not automatically 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'
source to share