Tkinter messagebox – showinfo() return value

Tkinter messagebox – showinfo() return value

In Tkinter, the messagebox.showinfo() method returns a string value of "ok".

Tkinter messagebox – showinfo() return value - Terminal output
Tkinter messagebox – showinfo() return value – Terminal output

To get the return value of Tkinter messagebox showinfo() method, assign a variable with the showinfo() method call.

For example,

result = messagebox.showinfo("Greeting", "Hello! Welcome to PythonExamples.org!")

Now, we can access the value returned by showinfo() method.

In this tutorial, you will learn how to save the value returned by messagebox.showinfo() method in Tkinter, print the returned value, and also print the type of the returned value, with example.

Examples

1. Print the return value of showinfo() method

For example, consider that you have created a GUI application using Python Tkinter, and you would like to greet the user with a message “Hello! Welcome to PythonExamples.org!” using messagebox.showinfo() method.

The method displays a box with an OK button. When user clicks on the OK button, the method returns a string value. We shall store that returned value in a variable result, print the value in it, and also print the type of the value using type() built-in function.

Python Program

import tkinter as tk
from tkinter import messagebox

# Create the main window
window = tk.Tk()

# Get returned value from showinfo()
result = messagebox.showinfo("Greeting", "Hello! Welcome to PythonExamples.org!")
print(result)
print(type(result))

# Run the application
window.mainloop()

Output

In Windows

In MacOS

Tkinter messagebox – showinfo() return value

If user clicks on OK button, then we get the following output in Terminal.

Tkinter showinfo() return value - User clicks on OK button - Terminal output
User clicks on OK – Terminal output

Summary

In this Python Tkinter tutorial, we learned how to get the return value of messagebox.showinfo() method in Tkinter, with examples.

Code copied to clipboard successfully 👍