- Python GUI Programming Cookbook(Second Edition)
- Burkhard A. Meier
- 221字
- 2021-07-09 19:51:06
How it works...
We added another callback function and attached it as a delegate to handle the click event. Now, when we click the Help | About menu, an action takes place. We are creating and displaying the most common pop-up message box dialogs. They are modal, so the user can't use the GUI until they click the OK button.
In the first example, we display an information box, as can be seen by the icon to its left. Next, we create warning and error message boxes, which automatically change the icon associated with the popup. All we have to do is specify which message box we want to display.
There are different message boxes that display more than one OK button, and we can program our responses according to the user's selection.
The following is a simple example that illustrates this technique:

Running this GUI code results in a popup whose user response can be used to branch on the answer of this event-driven GUI loop, by saving it in the answer variable:

The console output using Eclipse shows that clicking the Yes button results in the Boolean value of True being assigned to the answer variable:

For example, we could use the following code:
If answer == True:
<do something>
Clicking No returns False and Cancel returns None.