Saturday, August 25, 2012

Tkinter Message Box Demo

This code is based on the Tcl msgbox.tcl demo. Select an icon and type then press the Show Message Box to see the configured message box.


# File: msgbox.py
#    http://hg.python.org/cpython/file/4e32c450f438/Lib/tkinter/messagebox.py

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

from demopanels import MsgPanel, SeeDismissPanel

class MsgBoxDemo(ttk.Frame):
    
    def __init__(self, isapp=True, name='msgboxdemo'):
        ttk.Frame.__init__(self, name=name)
        self.pack(expand=Y, fill=BOTH)
        self.master.title('Message Box Demo')
        self.isapp = isapp
        self._create_widgets()
        
    def _create_widgets(self):
        if self.isapp:
            MsgPanel(self, 
                     ["Choose the icon and type option for the message box.\n",
                      "Press the 'Show Message Box' button to see an example ",
                      "message box based on your choices."])
            
            SeeDismissPanel(self)
        
        self._create_demo_panel()
        
    def _create_demo_panel(self):
        demoPanel = Frame(self)
        demoPanel.pack(side=TOP, fill=BOTH, expand=Y)
        
        bottom = Frame(demoPanel)
        bottom.pack(side=BOTTOM, fill=X)
        left = Frame(demoPanel)
        right = Frame(demoPanel)
        left.pack(side=LEFT, expand=True, padx=10, pady=10, fill=BOTH)
        right.pack(side=LEFT, expand=True, padx=10, pady=10, fill=BOTH)

        # Msg Button
        ttk.Button(bottom, text='Show Message Box', 
                   command=self._show_msg).pack(side=TOP)

        # Message box Icon panel
        ttk.Label(left, text='Icon').pack(side=TOP)
        ttk.Separator(left, orient=HORIZONTAL).pack(side=TOP, fill=X, expand=False)
        self.__iconVar = StringVar()
        for item in ('error', 'info', 'question', 'warning'):
            rb = ttk.Radiobutton(left, text=item, variable=self.__iconVar,
                                 value=item)
            rb.pack(side=TOP, pady=2, padx=10, anchor=W, fill=X)
        self.__iconVar.set('info')

        # Message box Type panel
        ttk.Label(right, text='Type').pack(side=TOP)
        ttk.Separator(right, orient=HORIZONTAL).pack(side=TOP, fill=X, expand=False)
        self.__typeVar = StringVar()
        for item in ('abortretryignore', 'ok', 'okcancel', 
                     'retrycancel', 'yesno', 'yesnocancel'):
            rb = ttk.Radiobutton(right, text=item, variable=self.__typeVar,
                                 value=item)
            rb.pack(side=TOP, pady=2, padx=10, anchor=W, fill=X)
        self.__typeVar.set('ok')
        
    def _show_msg(self):
        # triggered when user presses the 'Show Message Box' button
        mbicon = self.__iconVar.get()
        mbtype = self.__typeVar.get()
        
        msg = "This is a message box of type '{}', with icon '{}'".format(mbtype, mbicon)
        
        mb = mbox.Message(parent=self, title='Message Box', message=msg,
                          icon=mbicon, type=mbtype)
        res = mb.show()
        mbox.Message(self, icon='info', type='ok',
                     title='Message Box Selection',
                     message='You clicked on {}'.format(res)).show()
                
if __name__ == '__main__':
    MsgBoxDemo().mainloop()