In this article we want to talk about Complete Guide on Python TKinter Message Box, Python Tkinter Message Box is builtin Python module that provides easy interface for creating message boxes, message boxes are pop up windows that display messages to the user. these message boxes can be used to provide alerts, notifications, confirmations or to get user input. In this article we go through complete guide on Python Tkinter Message Box, covering its different types, options and use cases.
Types of Message Boxes
Tkinter provides several types of message boxes that can be used based on the required functionality:
- Showinfo: this type of message box is used to display information to users.
- Showwarning: this type of message box is used to display warning to users.
- Showerror: this type of message box is used to display an error message to users.
- Askyesno: this type of message box is used to ask users yes or no question.
- Askyesnocancel: this type of message box is used to ask users yes, no or cancel question.
- Askokcancel: this type of message box is used to ask users ok or cancel question.
- Askretrycancel: this type of message box is used to ask usesr retry or cancel question.
Options in Message Boxes
Following options can be passed while creating a message box:
- Title: title of message box.
- Message: message to be displayed in message box.
- Icon: icon to be displayed in message box.
- Default: default button selected in message box.
- Parent: parent window of message box.
Now let’s create a simple example on Python TKinter MessageBox and we want to create information messagebox.
1 2 3 4 5 6 7 |
import tkinter as tk from tkinter import messagebox root = tk.Tk() root.withdraw() messagebox.showinfo(title="GeeksCoders", message="Learn Complete Programming \n " "In GeeksCoders.com") |
In the above code we have imported tkinter and messagebox modules. after that we have created new instance of Tk class and withdraw the window to prevent it from showing up. we then use showinfo function from messagebox module to create new message box with title and message.
Run the complete code and this will be the result.
These are some more examples on Python TKinter MessageBox
Askyesno Example
1 2 3 4 5 6 7 8 9 10 |
import tkinter as tk from tkinter import messagebox root = tk.Tk() root.withdraw() response = messagebox.askyesno(title="Question", message="Do you like geekscoders.com") if response == 1: print("User clicked Yes") else: print("User clicked No") |
In this example we have used askyesno function from messagebox module to create new message box with yes or no question. if user clicks Yes, the message User clicked Yes is printed to console. if user clicks No message User clicked No is printed to the console.
Run the complete code and this will be the result
Askretrycancel Example
1 2 3 4 5 6 7 8 9 10 |
import tkinter as tk from tkinter import messagebox root = tk.Tk() root.withdraw() response = messagebox.askretrycancel(title="Error", message="An error occurred. Do you want to retry?") if response == 1: print("User clicked Retry") else: print("User clicked Cancel") |
In this example we have used askretrycancel function from the messagebox module to create new message box with retry or cancel question. if the user clicks Retry, the message “User clicked Retry” is printed to the console. If the user clicks Cancel, the message “User clicked Cancel” is printed to the console.
Run the complete code and this will be the result.
PySide6 GUI Articles
- How to Create Label in PySide6
- How to Create Button in Python & PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
So now Python Tkinter Message Box provides an easy way for creating pop up message boxes in Python applications. these message boxes can be used for different purposes, for example you can use messageboxes for displaying alerts, notifications and getting user input.