A message box is a pop-up window that gives feedback to the user. It can be informational, hinting at potential problems, and even catastrophic errors. Learn the difference between each of them.
- [Instructor] Hi again. Welcome back to a new section of this course, Look and Feel Customization. Previously, we talked about layout management of our GUI. In this section, we will customize our GUI using Python 3. Specifically, we'll discuss creating basic message boxes and then independent message boxes. We'll see how to change the properties of a TkInter window form. We'll then move on to using a spin box control widget.
We'll finally add some extra features to our GUI. Now, we move on to the first video of this section that deals with creating message boxes. In the first example, we will display an information box which can be identified by the icon to the left of it. Next, we create warning and error message boxes that automatically change the icon associated with the pop-up. A message box is a pop-up window that gives feedback to the user.
It could be informational, hinting at potential problems and even catastrophic errors. Using Python to create message boxes is very easy. We will add functionality to the Help/About menu item we created in the previous video. The typical feedback to the user when clicking the Help/About menu in most applications is informational. We start with this information, and then vary the design pattern to show warnings and errors.
Let's see how to do it. The file that we'll write our code in is called messagebox.py. I've added this line of code to the top of the module where the import statements live. Next, we've created a callback function that will display a message box. We have to locate the code of the callback above the code, where we attached the callback to the menu item, because this is still procedural, and not OOP code.
We've added this code just above the lines where we create the help menu. Let's check our output. Clicking Help and then About, it causes the following pop-up window to appear, which is an information box. Nice. The next task, transforming this code into a warning message box pop-up window instead. I've copied the entire code of messagebox.py and renamed it as warningbox.py.
We'll comment out this line, and add another one instead. The showwarning method will display a warning box. Let's see what happens. Running with code will now result in a slightly modified message box. Great. Displaying an error message box is simple, and usually warns the user of a serious problem. Let's make some more changes. Now, comment out this line, and add another one which uses showerror method.
Let's check the difference in the output. We can now see an error message box instead of a warning. Nice. We have added another callback function and attached it as a delegate to handle the click event. When we click the Help/About menu, an action takes place. We're creating and displaying the most common pop-up message box dialogs bare modal, so the user can't use the GUI until they click the OK button. All we have to do is specify which mbox 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. Here's a simple example that illustrates this technique. I've created this into another file called dualchoicebox.py. Running this GUI code results in a pop-up 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 shows that clicking the Yes button results in the Boolean value of true being assigned to the answer variable. And we can use it to do something. That's about it for this video. We covered all there is to know about message boxes in this video. In the next video, we'll see how to code independent message boxes.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Creating buttons and widgets
- Adding labels and features
- Expanding a GUI dynamically
- Aligning frames and embedding frames
- Creating menu bars, message boxes, and tooltips
- Using module-level global variables
- Coding in classes
- Using Matplotlib to create charts
- Working with multiple threads, queues, and TCP/IP
- Using URLOpen to read data from websites
- Localizing a GUI and preparing for internationalization
- Testing a GUI using unit tests and Eclipse PyDev IDE
- Using the wxPython library
- Using Tkinter, PyOpenGL, and Pyglet