Using the tkinter.messagebox library

Previous entries in this series:

  1. Welcome to tkinter
  2. Questions and answers for a basic tkinter GUI
  3. Feet to Meters using tkinter and ttk

While I am mostly looking at the ttk module at present, there is a module in the tkinter library that is so useful that I want to take a break and cover it. The module is called messagebox. It is located in the tkinter library.

To import the messagebox library into your program, do one of the following:

from tkinter import messagebox

Or, if you don’t want to preface call to messagebox by typing ‘messagebox’, you can import it with my preferred method:

import tkinter.messagebox as mbox

The advantage of using the second method of importing messagebox is that you can preface all of its calls like this:

mbox.askokcancel( ... )

In tkinter, message boxes are relatively straightforward items. When they are called, they will create the message window, display the information you provide to them, and return a value that contains the answer.

You can call the message box like this:

value = mbox.showinfo(title='title', message='This is the message.', detail='Optional details')
# or messagebox.showinfo( ... ) if you didn't
# import tkinter.messagebox as mbox

When the message box returns, all you have to do is evaluate the value to know what the user selected.

I have written a Python program that explores the different message boxes as well as the properties that each of the message boxes returns. I believe that it is a useful reference to the various kinds of message boxes that is easier to understand than just having everything written out in a manual.

Simply copy and paste the code into your own file, or you can download the file from my Github project called MatchingTiePy that contains various pieces of sample code along with trinkets that I think are useful.

Without further ado, here is the complete ‘messageboxes.py’ program:

# filename: messageboxes.py

import tkinter as tk
from tkinter import ttk
import tkinter.messagebox as mbox

class MainWindow(tk.Tk):
    def destroy(self):
        self.value = mbox.askyesno(title='Quit', message='Do you really want to quit?', default=mbox.YES)
        if self.value == True:
            super().destroy()

class App():
    root = MainWindow()
    primary_frame = ttk.Frame(root)
    style = ttk.Style()
    def __init__(self, *args, **kwargs):
        self.configure_style()
        self.configure_root()
        self.configure_primary_frame()
        self.create_primary_frame_widgets()

    def configure_style(self):
        ttk.Style().configure('.', background='#add8e6', foreground='#000000', font='Times 16')

    def configure_root(self):
        App.root.rowconfigure(0, weight=1)
        App.root.columnconfigure(0, weight=1)
        App.root.title('Messagebox Demonstration')
        App.root.grid()

    def configure_primary_frame(self):
        for row in range(4):
            App.primary_frame.rowconfigure(row, weight=1)
        for col in range(5):
            App.primary_frame.columnconfigure(col, weight=1)
        App.primary_frame.grid(sticky=tk.NSEW)

    def create_primary_frame_widgets(self):
        ttk.Label(master=App.primary_frame, text='This row returns \'ok\':').grid(row=0, column=0, sticky=tk.E, padx=10, pady=20)
        ttk.Button(master=App.primary_frame, text='showinfo', command=self.showinfo_command).grid(row=0, column=1, sticky=tk.EW)
        ttk.Button(master=App.primary_frame, text='showwarning', command=self.showwarning_command).grid(row=0, column=2, sticky=tk.EW)
        ttk.Button(master=App.primary_frame, text='showerror', command=self.showerror_command).grid(row=0, column=3, sticky=tk.EW)
        ttk.Label(master=App.primary_frame, text='This row returns \'yes\' and \'no\':').grid(row=1, column=0, sticky=tk.E, padx=10, pady=20)
        ttk.Button(master=App.primary_frame, text='askquestion', command=self.askquestion_command).grid(row=1, column=1, sticky=tk.EW)
        ttk.Label(master=App.primary_frame, text='This row returns True, False, and None:').grid(row=2, column=0, sticky=tk.E, padx=10, pady=20)
        ttk.Button(master=App.primary_frame, text='askokcancel', command=self.askokcancel_command).grid(row=2, column=1, sticky=tk.EW)
        ttk.Button(master=App.primary_frame, text='askyesno', command=self.askyesno_command).grid(row=2, column=2, sticky=tk.EW)
        ttk.Button(master=App.primary_frame, text='askyesnocanel', command=self.askyesnocancel_command).grid(row=2, column=3, sticky=tk.EW)
        ttk.Button(master=App.primary_frame, text='askretrycancel', command=self.askretrycancel_command).grid(row=2, column=4, sticky=tk.EW)
        ttk.Button(master=App.primary_frame, text='Quit', command=App.root.destroy).grid(row=3, column=1, columnspan=2, sticky=tk.EW, ipady=20)

    def showinfo_command(self):
        self.value = mbox.showinfo(title='showinfo', message='This is the \'messagebox.showinfo\' message box.', detail='It returns \'ok\'')
        print('messagebox.showinfo returned: ', self.value)

    def showwarning_command(self):
        self.value = mbox.showwarning(title='showwarning', message='This is the \'messagebox.showwarning\' message box.', detail='It returns \'ok\'')
        print('messagebox.showwarning returned: ', self.value)

    def showerror_command(self):
        self.value = mbox.showerror(title='showerror', message='This is the \'messagebox.showerror\' message box.', detail='It returns \'ok\'')
        print('messagebox.showerror returned: ', self.value)

    def askquestion_command(self):
        self.value = mbox.askquestion(title='askquestion', message='This is the \'messagebox.askquestion\' message box with the optional argument of default=\'no\'.', detail='It returns \'yes\' or \'no\'', default=mbox.NO)
        print('messabebox.askquestion returned: ', self.value)

    def askokcancel_command(self):
        self.value = mbox.askokcancel(title='askokcancel', message='This is the \'messagebox.askokcancel\' message box with the optional argument of default=\'cancel\'.', detail='It returns True or False', default=mbox.CANCEL)
        print('messagebox.askokcancel returned: ', self.value)

    def askyesno_command(self):
        self.value = mbox.askyesno(title='askyesno', message='This is the \'messagebox.askyesno\' message box with the optional argument of default=\'no\'.', detail='It returns True or False', default=mbox.NO)
        print('messagebox.askyesno returned: ', self.value)

    def askyesnocancel_command(self):
        self.value = mbox.askyesnocancel(title='askyesnocancel', message='This is the \'messagebox.askyesnocancel\' message box with the optional argument of default=\'cancel\'.', detail='It returns True, False, or None', default=mbox.CANCEL)
        print('messagebox.askyesnocancel returned: ', self.value)

    def askretrycancel_command(self):
        self.value = mbox.askretrycancel(title='askretrycancel', message='This is the \'messagebox.askretrycancel\' message box with the optional argument of default=\'cancel\'.', detail='It returns True or False', default=mbox.CANCEL)
        print('messagebox.askretrycancel returned: ', self.value)

if __name__ == '__main__':
    app = App()
    App.root.mainloop()
Advertisements

Published by

Marisa

I am a writer of words, a thinker of thoughts, a changer of genders, and a queerer of life. I am an antagonist of the ordinary; and while I do tolerate it, I also look at it with contempt.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s