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.
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:
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:
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)
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:
tkMessageBox
into our current namespace.about
function to display a showinfo
message box.help_box
function to display a showinfo
message box.exit
command with an askokcancel
box. If the user clicks on OK, the exit
command destroys the root window to close the window.exit
command.command
callbacks to About, Help, and Exit menu items.18.116.50.87