The Tkinter tkMessageBox has various methods to display a message box.
There is a slight difference between Tkinter for Python 2.7 and Python 3.
To find your Python version try one of these commands:
TkMessage boxTo show a minimalistic Tkinter message box, use the function showinfo() where the parameters are the window title and text.
The showinfo() function is in a different module depending on the Python version.
Python 3.x
1 2 3 4 5 | from tkinter import messagebox messagebox.showinfo("Title", "a Tk MessageBox") |
Python 2.7
1 2 3 4 5 6 | import Tkinter import tkMessageBox tkMessageBox.showinfo("Title", "a Tk MessageBox") |
Tkinter showerror, showwarning and showinfo
Tk messagebox dialog
Tkinter includes several other message boxes:
- showerror()
- showwarning()
- showinfo()
Python 3.x
1 2 3 4 5 6 7 8 9 10 11 12 13 | import tkinter from tkinter import messagebox # hide main window root = tkinter.Tk() root.withdraw() # message box display messagebox.showerror("Error", "Error message") messagebox.showwarning("Warning","Warning message") messagebox.showinfo("Information","Informative message") |
Python 2.7
1 2 3 4 5 6 7 8 9 10 11 12 13 | import Tkinter import tkMessageBox # An error box tkMessageBox.showerror("Error","No disk space left on device") # A warning box tkMessageBox.showwarning("Warning","Could not start service") # An information box tkMessageBox.showinfo("Information","Created in Python.") |
Python Tkinter Message Box Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from tkinter import * from tkinter import messagebox ws = Tk() ws.title('Code4Example') ws.geometry('300x200') ws.config(bg='#5FB691') def msg1(): messagebox.showinfo('information', 'Hi! You got a prompt.') messagebox.showerror('error', 'Something went wrong!') messagebox.showwarning('warning', 'accept T&C') messagebox.askquestion('Ask Question', 'Do you want to continue?') messagebox.askokcancel('Ok Cancel', 'Are You sure?') messagebox.askyesno('Yes|No', 'Do you want to proceed?') messagebox.askretrycancel('retry', 'Failed! want to try again?') Button(ws, text='Click Me', command=msg1).pack(pady=50) ws.mainloop() |