Computer >> Computer tutorials >  >> Programming >> Python

Different messages in Tkinter - Python


Tkinter is the GUI module of python. It uses various message display options which are in response to the user actions or change in state of a running program. The message box class is used to display variety of messages like confirmation message, error message, warning message etc.

Example-1

The below example shows display of a message with the background colour, font size and colour etc customizable.

import tkinter as tk
main = tk.Tk()

key = "the key to success is to focus on goals and not on obstacles"

message = tk.Message(main, text = key)
message.config(bg='white', font=('times', 32, 'italic'))

message.pack()
tk.mainloop()

Running the above code gives us the following image −

Different messages in Tkinter - Python

Example-2

In the below examples we see the display of many standard actions like showing information or showing an error etc. There are different functions of the messagebox class that are used to show various message categories.

The Question Box

This is achieved by using askquestion() function.

Example

from tkinter.messagebox import *
print(askquestion("Question", "Proceed to next Step?"))

Running the above code gives us the following result −

Different messages in Tkinter - Python

The Retry Box

This is achieved by using askretrycancel() function.

from tkinter.messagebox import *
print(askretrycancel("Retry", "Try Again?"))

Running the above code gives us the following result −

Different messages in Tkinter - Python

The Error Box

This is achieved by using showerror() function.

from tkinter.messagebox import *
print(showerror("Error", "Error in checkout"))

Running the above code gives us the following result −

Different messages in Tkinter - Python

The Warning Box

This is achieved by using askretrycancel() function.

from tkinter.messagebox import *
print(showwarning("Warning", "This may result in delay !"))

Running the above code gives us the following result −

Different messages in Tkinter - Python