Reader small image

You're reading from  Python GUI Programming Cookbook. - Third Edition

Product typeBook
Published inOct 2019
Reading LevelIntermediate
Publisher
ISBN-139781838827540
Edition3rd Edition
Languages
Right arrow
Author (1)
Burkhard Meier
Burkhard Meier
author image
Burkhard Meier

Burkhard Meier is a professional software test automation designer, developer, and analyst. He has more than 17 years' professional experience working for several software companies in California, USA. He is the author of Python GUI Programming Cookbook, First and Second Edition. This book is also available as a Packt video course. He is also the author of the Python Projects Packt video course. In his professional career, he developed advanced in-house testing frameworks written in Python 3. He also developed advanced test automation GUIs in Python, which highly increased the productivity of the software development testing team. When not dreaming in Python code, he reads programming books about design, likes to go for long walks, and reads classical poetry.
Read more about Burkhard Meier

Right arrow

Look and Feel Customization

In this chapter, we will customize some of the widgets in our GUI by changing some of their attributes. We will also introduce a few new widgets that tkinter offers us.

In the Creating tooltips using Python recipe, we will create a ToolTip OOP-style class, which will be a part of the single Python module that we have been using until now.

You will learn how to create different message boxes, change the GUI window title, and much more. We will be using a spin box control to learn how to apply different styles.

Look and feel customization is a very important part of GUI design because it makes our GUI look professional.

Here is the overview of the Python modules for this chapter:

In this chapter, we will customize our GUI using Python 3.7 and above. We will cover the following recipes:

  • Creating message boxes – the information, warning, and error...

Creating message boxes – information, warning, and error

A message box is a pop-up window that gives feedback to the user. It can be informational, hinting at potential problems, as well as catastrophic errors.

Using Python to create message boxes is very easy.

Getting ready

We will add functionality to the Help | About menu item we created in Chapter 2, Layout Management, in the Creating tabbed widgets recipe.

The code is from GUI_tabbed_all_widgets_both_tabs.py. The typical feedback to the user when clicking the Help | About menu in most applications is informational. We'll start with this information and then vary the design pattern to show warnings and errors.

...

How to create independent message boxes

In this recipe, we will create our tkinter message boxes as standalone top-level GUI windows.

You will first notice that, by doing so, we end up with an extra window, so we will explore ways to hide this window.

In the previous recipe, we invoked tkinter message boxes via our Help | About menu from our main GUI form.

So, why would we wish to create an independent message box?

One reason is that we might customize our message boxes and reuse them in several of our GUIs. Instead of having to copy and paste the same code into every Python GUI we design, we can factor it out of our main GUI code. This creates a small reusable component, which we can then import into different Python GUIs.

Getting ready

...

How to create the title of a tkinter window form

The principle of changing the title of a tkinter main root window is the same as we discussed in the previous recipe: How to create independent message boxes. We just pass in a string as the first argument to the constructor of the widget.

Getting ready

Instead of a pop-up dialog window, we create the main root window and give it a title.

How to do it...

The following code creates the main window and adds a title to it. We have already done this in the previous recipes; for example, in the Creating tabbed widgets recipe...

Changing the icon of the main root window

One way to customize our GUI is to give it an icon different from the default icon that ships out of the box with tkinter. Here is how we do this.

Getting ready

We are improving our GUI from the Creating tabbed widgets recipe in Chapter 2, Layout Management. We will use an icon that ships with Python, but you can use any icon you find useful. Make sure you have the full path to where the icon lives in your code, or you might get errors.

How to do it...

For this example, I have copied the icon from where I installed Python 3.7 to...

Using a spin box control

In this recipe, we will use a Spinbox widget, and we will also bind the Enter key on the keyboard to one of our widgets. The Spinbox widget is a one-line widget, like the Entry widget, with the additional capability to restrict the values it will display. It also has some small up/down arrows to scroll up and down between the values.

Getting ready

We will use our tabbed GUI, from the How to create the title of a tkinter window form recipe, and add a Spinbox widget above the ScrolledText control. This simply requires us to increment the ScrolledText row value by one and insert our new Spinbox control in the row above the Entry widget.

...

Applying relief – the sunken and raised appearance of widgets

We can control the appearance of our Spinbox widgets by using an attribute that makes them appear in different formats, such as sunken or raised. This attribute is the relief attribute.

Getting ready

We will add one more Spinbox control to demonstrate the available appearances of widgets, using the relief attribute of the Spinbox control.

How to do it...

While we are creating the second Spinbox, let's also increase borderwidth to distinguish our second Spinbox from the first Spinbox:

  1. Open GUI_spinbox_small_bd_scrol_values...

Creating tooltips using Python

This recipe will show you how to create tooltips. When the user hovers the mouse over a widget, additional information will be available in the form of a tooltip.

We will code this additional information into our GUI.

Getting ready

We will be adding more useful functionality to our GUI. Surprisingly, adding a tooltip to our controls should be simple, but it is not as simple as we'd want it to be.

In order to achieve this desired functionality, we will place our tooltip code in its own OOP class.

How to do it...

These are the steps to...

Adding Progressbar to the GUI

In this recipe, we will add a Progressbar to our GUI. It is very easy to add a ttk.Progressbar, and we will demonstrate how to start and stop a Progressbar. This recipe will also show you how to delay the stopping of a Progressbar, and how to run it in a loop.

A Progressbar is typically used to show the current status of a long-running process.

Getting ready

We will add Progressbar to Tab 2 of the GUI that we developed in a previous recipe: Using a spin box control.

How to do it...

Here are the steps to create a Progressbar and some new Buttons...

How to use the canvas widget

This recipe shows how to add dramatic color effects to our GUI by using the tkinter canvas widget.

Getting ready

We will improve our previous code from GUI_tooltip.py, and we'll improve the look of our GUI by adding some more colors to it.

How to do it...

First, we will create a third tab in our GUI in order to isolate our new code.

Here is the code to create the new third tab:

  1. Open GUI_tooltip.py and save the module as GUI_canvas.py.
  2. Create a third tab control:
tabControl = ttk.Notebook(win)          # Create Tab Control 

tab1 = ttk...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Python GUI Programming Cookbook. - Third Edition
Published in: Oct 2019Publisher: ISBN-13: 9781838827540
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Burkhard Meier

Burkhard Meier is a professional software test automation designer, developer, and analyst. He has more than 17 years' professional experience working for several software companies in California, USA. He is the author of Python GUI Programming Cookbook, First and Second Edition. This book is also available as a Packt video course. He is also the author of the Python Projects Packt video course. In his professional career, he developed advanced in-house testing frameworks written in Python 3. He also developed advanced test automation GUIs in Python, which highly increased the productivity of the software development testing team. When not dreaming in Python code, he reads programming books about design, likes to go for long walks, and reads classical poetry.
Read more about Burkhard Meier