How to create a message box with Tkinter?

TkinterServer Side ProgrammingProgramming

In a particular application, we can create messagebox using messagebox method. Here is the list of messagebox we can create for a particular application,

  • showinfo() - to show a general message on the screen.

  • showwarning() - to show the warning to the user.

  • showerror() - Display error message.

  • askquestion() - Query the user through the messagebox.

  • asktocancel() - Display the info to cancel an operation.

  • askretrycancel() - Display the message to prompt the user to retry again or not.

Example

In this example, we will create an application that will show an info message box after clicking a button.

#Import required libraries
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

#Create an instance of tkinter frame
win= Tk()

#Define the geometry of the window
win.geometry("750x250")

#Define a function to show the messagebox
def handler():
   messagebox.showinfo("Message", "You have clicked a Button")

#Create a Label
Label(win, text= "Click the below button", font=('Helvetica 16
underline')).pack(pady=20)

#Create a Button
ttk.Button(win, text= "Click", command= handler).pack(pady=30)
win.mainloop()

Output

When we click the Button, it will show a messsagebox,


raja
Published on 03-May-2021 09:30:53
Advertisements