Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python GUI Programming Cookbook

You're reading from   Python GUI Programming Cookbook Develop functional and responsive user interfaces with tkinter and PyQt5

Arrow left icon
Product type Paperback
Published in Oct 2019
Publisher
ISBN-13 9781838827540
Length 486 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Burkhard Meier Burkhard Meier
Author Profile Icon Burkhard Meier
Burkhard Meier
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Creating the GUI Form and Adding Widgets 2. Layout Management FREE CHAPTER 3. Look and Feel Customization 4. Data and Classes 5. Matplotlib Charts 6. Threads and Networking 7. Storing Data in Our MySQL Database via Our GUI 8. Internationalization and Testing 9. Extending Our GUI with the wxPython Library 10. Building GUIs with PyQt5 11. Best Practices 12. Other Books You May Enjoy

Setting the focus to a widget and disabling widgets

While our GUI is nicely improving, it would be more convenient and useful to have the cursor appear in the Entry widget as soon as the GUI appears.

In this recipe, we learn how to make the cursor appear in the Entry box for immediate text Entry rather than the need for the user to click into the Entry widget to give it the focus method before typing into the entry widget.

Getting ready

This recipe extends the previous recipe, Creating textbox widgets. Python is truly great. All we have to do to set the focus to a specific control when the GUI appears is call the focus() method on an instance of a tkinter widget we previously created. In our current GUI example, we assigned the ttk.Entry class instance to a variable named name_entered. Now, we can give it the focus.

How to do it...

Place the following code just above the previous code, which is located at the bottom of the module, and which starts the main window's event loop, like we did in the previous recipes:

  1. Start with the GUI_textbox_widget.py module and save it as GUI_set_focus.py.
  2. Use the name_entered variable we assigned the ttk Entry widget instance to and call the focus() method on this variable:
name_entered.focus()

The preceding instructions produce the following code (GUI_set_focus.py):

  1. Run the code and observe the output.

If you get some errors, make sure you are placing calls to variables below the code where they are declared. We are not using OOP as of now, so this is still necessary. Later, it will no longer be necessary to do this.

On a Mac, you might have to set the focus to the GUI window first before being able to set the focus to the Entry widget in this window.

Adding line 38 of the Python code places the cursor in our text Entry widget, giving the text Entry widget the focus. As soon as the GUI appears, we can type into this textbox without having to click it first. The resulting GUI now looks like this, with the cursor inside the Entry widget:

Note how the cursor now defaults to residing inside the text entry box.

We can also disable widgets. Here, we are disabling the button to show the principle. In larger GUI applications, the ability to disable widgets gives you control when you want to make things read only. Most likely, those would be combobox widgets and Entry widgets, but as we have not yet gotten to those widgets yet, we will use our button.

To disable widgets, we will set an attribute on the widget. We can make the button disabled by adding the following code below line 37 of the Python code to create the button:

  1. Use the GUI_set_focus.py module and save it as GUI_disable_button_widget.py.
  2. Use the action button variable to call the configure method and set the state attribute to disabled:
action.configure(state='disabled')
  1. Call the focus() method on the name_entered variable:
name_entered.focus()

The preceding instructions produce the following code (GUI_disable_button_widget.py):

  1. Run the code. After adding the preceding line of Python code, clicking the button no longer creates an action:

Let's go behind the scenes to understand the code better.

How it works...

This code is self-explanatory. In line 39, we set the focus to one control, and in line 37, we disable another widget. Good naming in programming languages helps to eliminate lengthy explanations. Later in this book, there will be some advanced tips on how to do this while programming at work or practicing our programming skills at home.

We've successfully learned how to set the focus to a widget and disable widgets. Now, let's move on to the next recipe.

lock icon The rest of the chapter is locked
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.
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 $19.99/month. Cancel anytime