Working with message boxes

In this iteration, let's complete our code for the About and Help menus. The functionality is simple. When a user clicks on the Help or About menu, it pops up a message window and waits for the user to respond by clicking on a button. While we can easily code new Toplevel windows to show our About and Help popup windows, we will instead use a module called tkMessageBox to achieve this functionality. This is because the module provides an efficient way to handle this and similar functionalities with minimal coding.

We will also complete coding the Exit button functioning in this iteration. Currently, when a user clicks on the Close button, the window is simply closed. We want to ask the user if they really want to quit or have they clicked on the Close button accidentally.

Prepare for Lift Off

The tkMessageBox module provides ready-made message boxes to display a wide variety of messages in your applications. Some of these functions are showinfo, showwarning, showerror, askquestion, askyesno, askokcancel, and askretryignore. These are illustrated, when in use, in the following screenshot:

Prepare for Lift Off

To use the module, we simply import it into the current namespace as shown in the following command:

import tkMessageBox

A demonstration of commonly-used functions of tkMessageBox is illustrated in 2.08.py. Some common usage patterns are mentioned as follows:

tkMessageBox.showwarning("Beware", "You are warned")
tkMessageBox.showinfo("FYI", "This is FYI")
tkMessageBox.showerror("Err..", "its leaking.")
tkMessageBox.askquestion("?", "Can you read this ?")
tkMessageBox.askokcancel("OK", "Quit Postponing ?")
tkMessageBox.askyesno("Yes or No", " What Say ?")    
tkMessageBox.askretrycancel("Retry", "Load Failed")

Using this module to display messages has the following advantages:

  • Minimal coding yields functional features
  • The messages can easily be configured
  • Messages are presented with icons
  • It presents a standardized view of common messages on each platform

Engage Thrusters

  1. Let us now code the about and help functions for our code editor. The use case is simple. When a user clicks on the About menu, it pops up a message with the OK button. Similarly, when the user clicks on the Help button, they are also prompted with a message with the OK button.

    To achieve these functionalities, we include the following code in our editor. (See the code in 2.09.py)

    import tkMessageBox
    def about(event=None):
      tkMessageBox.showinfo("About","Tkinter GUI Application
     Development Hotshot")
    
    def help_box(event=None):
      tkMessageBox.showinfo("Help","For help refer to book:
    
      Tkinter GUI Application
     Development Hotshot ", icon='question')
    
    aboutmenu.add_cascade(label="Help", command=help_box)
  2. Next, we will look at adding the Quit Confirmation feature. When the user clicks on File | Exit, it prompts an Ok-Cancel dialog to confirm the quit action.
    def exit_editor(event=None):
      if tkMessageBox.askokcancel("Quit", "Do you really want to quit?"):
        root.destroy()
    root.protocol('WM_DELETE_WINDOW', exit_command) # override close 
    filemenu.add_command(label="Exit", accelerator='Alt+F4', command=exit_editor)

    The description of the code is listed as follows:

    • First we import tkMessageBox into our current namespace.
    • We then define our about function to display a showinfo message box.
    • Similarly, we define our help_box function to display a showinfo message box.
    • We then define the exit command with an askokcancel box. If the user clicks on OK, the exit command destroys the root window to close the window.
    • We then override the close button protocol and redirect it to be handled by our definition of the exit command.
    • Finally, we add command callbacks to About, Help, and Exit menu items.

Objective Complete – Mini Briefing

In this iteration, we completed coding the functionality for the File | Exit, About | About, and About | Help menu items of our code editor. We also saw how to use the tkMessageBox module to display different message boxes for some commonly-used message formats.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.50.87