Tkinter: messagebox, aka say something loud!

If you are doing some GUI use of your programs, you will probably need to use, at least, sometime, a messagebox.

What is a messagebox?

You may ask. It is a pop up window like this:

It is one to the tool of tkinter, so we are going to make some example, as usual, trying not only to say “this is a messagebox.info()”, but also to show some simple practical use of them. At least I will try to.

How many types of messagebox tkinter supports

A lot.
We can have, infact, different type of messagebox:

  • showinfo()
  • showerror()
  • showwarning()
  • askyesno()
  • askokcancel()
  • askquestion()

Messagebox

1. showinfo(“Title”,”Message”)

We want to add a messagebox to the app. Let’s see how messagebox works.

import tkinter as tk
from tkinter import messagebox

# window, title and size
root = tk.Tk()
root.title("message")
root.geometry("400x400")

def message():
messagebox.showinfo("Ciao","Hello")

root.bind("<Button-1>", lambda x: message())

root.mainloop()

We will see how to create, modify and save new files from the app.

The output of the code for the messagebox

showwarning()

This message will show that something is happening and you should take care of that.

import tkinter as tk
from tkinter import messagebox

root = tk.Tk()
root.title("messagebox")

def message():
messagebox.showwarning("Be careful!")

root.bind("<Button-1>", lambda x: message())
root.mainloop()

error()

This is the message that tell you something is gone wrong.

import tkinter as tk
from tkinter import messagebox

root = tk.Tk()
root.title("messagebox")

def message():
messagebox.showerror("Ops!")


root.bind("<Button-1>", lambda x: message())
root.mainloop()

How to hide the window using the messagebox

In case you want to hide the main window, because you just want to use the messagebox (maybe into a script that uses the command line interface only and you want for some reason use the messagebox only as an alert or something),  you can use root.withdraw() or root.iconify(). The method iconify() has its opposite in root.deiconify(). If you want to get rid of the window at the end, use root.destroy(). Go to this stackoverflow question to get other interesting suggestions about hiding the main tkinter window.

import tkinter as tk
from tkinter import messagebox

# window, title and size
root = tk.Tk()
root.title("message")
root.geometry("400x400")

def message():
root.iconify() # Window iconify itself before the messagebox appears
messagebox.showinfo("Ciao","Hello")
root.deiconify() # windows return to original position and dimensions

root.bind("<Button-1>", lambda x: message())

root.mainloop()

In this example, the windows goes into the toolbar while the messagebox appears.

In this code, instead, the window will just disappear, without seeing that effect that makes you see the window go into the toolbar. At the end of the messageboxes (Error, Warning and Information), the window will be destroyed. So, e careful, if you use tkinter withdraw() function, the window is not visible, but will be there.

import tkinter as tk
from tkinter import messagebox

# window, title and size
root = tk.Tk()
root.title("message")
root.geometry("400x400")

def message():
root.withdraw()
# message box display
messagebox.showerror("Error", "Error message")
messagebox.showwarning("Warning","Warning message")
messagebox.showinfo("Information","Informative message")
root.destroy()
print("Window has been deleted")

root.bind("<Button-1>", lambda x: message())
root.mainloop()

messagebox: Ask yes or no

This code takes your choise among yes or no.

import tkinter as tk
from tkinter import messagebox

# window, title and size
root = tk.Tk()
root.title("message")
root.geometry("400x400")

def message():
root.overrideredirect(1)
root.withdraw()
# message box display
x = messagebox.askquestion("Do you like Python?")
if x == "yes":
print("So, you do like Python")
else:
print("Sorry to hear that")
print(f"You answered {x}")

root.destroy()
print("Window has been deleted")

root.bind("<Button-1>", lambda x: message())
root.mainloop()

In the console the output is this

messagebox: ok or cancel

This time you will be asked to say yes of cancel to confirm an action.

import tkinter as tk
from tkinter import messagebox

# window, title and size
root = tk.Tk()
root.title("message")
root.geometry("400x400")

def message():
root.overrideredirect(1)
root.withdraw()
# message box display
x = messagebox.askokcancel("Do you want to go on?")
if x:
print("Ok, go on reading")
else:
print("See ya soon, then")
print(f"You answered {x}")

root.destroy()
print("Window has been deleted")

root.bind("<Button-1>", lambda x: message())
root.mainloop()

Ask retry or cancel

With this you ask if you want to retry when something goes wrong. It returns True if you retry and False if you cancel.

import tkinter as tk
from tkinter import messagebox

root = tk.Tk()
root.title("messagebox")

def message():
x = messagebox.askretrycancel("Gone wrong!")
print(x)


root.bind("<Button-1>", lambda x: message())
root.mainloop()

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.