Home » Tkinter messagebox

Tkinter messagebox

by maxguy71

The tkMessageBox module is used to display message boxes in your applications.

This module provides a number of functions that you can use to display an appropriate message.

Syntax

Here is the syntax to create the Messagebox widget

tkMessageBox.FunctionName(title, message [, options])

Parameters

  • FunctionName − This is the name of the message box function.
  • title − This is the text to be displayed in the title bar of a message box.
  • message − This is the text to be displayed as a message.
  • options − options are alternative choices that you may use to tailor a standard message box. Some of the options that you can use are default and parent. The default option is used to specify the default button, such as ABORT, RETRY, or IGNORE in the message box. The parent option is used to specify the window on top of which the message box is to be displayed.

There are functions or methods that are available in the messagebox widget.

  1. showinfo(): Show some relevant information to the user.
  2. showwarning(): Display the warning to the user.
  3. showerror(): Display the error message to the user.
  4. askquestion(): Ask question and user has to answered in yes or no.
  5. askokcancel(): Confirm the user’s action regarding some application activity.
  6. askyesno(): User can answer in yes or no for some action.
  7. askretrycancel(): Ask the user about doing a particular task again or not.

 

Code

import Tkinter
import tkMessageBox

top = Tkinter.Tk()
def hello():
   tkMessageBox.showinfo("Say Hello", "Hello World")

B1 = Tkinter.Button(top, text = "Say Hello", command = hello)
B1.pack()

top.mainloop()

You can display many of the types of messageboxes with this example

from tkinter import *
from tkinter import messagebox

top = Tk()
top.title('messagebox examples')
top.geometry('300x300')
top.config(bg='#5FB691')

def msg1():
    messagebox.showinfo('information', 'Hi! You got a prompt.')
    messagebox.showerror('error', 'Something went wrong!')
    messagebox.showwarning('warning', 'accept T&C')
    messagebox.askquestion('Ask Question', 'Do you want to continue?')
    messagebox.askokcancel('Ok Cancel', 'Are You sure?')
    messagebox.askyesno('Yes|No', 'Do you want to proceed?')
    messagebox.askretrycancel('retry', 'Failed! want to try again?')

Button(top, text='Click Me', command=msg1).pack(pady=50)

top.mainloop()

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More