Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tkinter.messagebox.showinfo doesn't always work

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()
like image 661
v3nd3774 Avatar asked Apr 21 '15 14:04

v3nd3774


People also ask

What is Showinfo in tkinter?

showinfo() We use this messagebox function when we want to show some related or relevant information to the user. Let us try to create an information message box with an example below. Code: import tkinter from tkinter import messagebox top = tkinter.

What does from tkinter import Messagebox mean?

The tkMessageBox module is used to display message boxes in your applications. This module provides a number of functions that you can use to display an appropriate message. Some of these functions are showinfo, showwarning, showerror, askquestion, askokcancel, askyesno, and askretryignore.

How do I import a Tkmessage box?

import TkMessageBox => from tkinter import messagebox . Then use it like messagebox. askokcancel(...) .


1 Answers

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'
like image 116
TigerhawkT3 Avatar answered Oct 19 '22 07:10

TigerhawkT3