Search icon CANCEL
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 Over 80 object-oriented recipes to help you create mind-blowing GUIs in Python

Arrow left icon
Product type Paperback
Published in Dec 2015
Publisher
ISBN-13 9781785283758
Length 350 pages
Edition 1st Edition
Languages
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 FREE CHAPTER 2. Layout Management 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. Creating Amazing 3D GUIs with PyOpenGL and PyGLet 11. Best Practices Index

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. Here we learn how to do this.

Getting ready

This recipe extends the previous recipe.

How to do it...

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 we named nameEntered. Now we can give it the focus.

Place the following code just above the bottom of the module that starts the main windows event loop, just like in previous recipes. 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 yet, so this is still necessary. Later, it will no longer be necessary to do this.

nameEntered.focus()            # Place cursor into name Entry

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 this one line of Python code places the cursor into our text Entry box, giving the text Entry box the focus. As soon as the GUI appears, we can type into this text box without having to click it first.

How to do it...

Note

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

We can also disable widgets. To do that, we set a property on the widget. We can make the button disabled by adding this one line of Python code:

action.configure(state='disabled')    # Disable the Button Widget

After adding the above line of Python code, clicking the button no longer creates any action!

How to do it...

How it works...

This code is self-explanatory. We set the focus to one control and 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.

There's more...

Yes. This is only the first chapter. There is much more to come.

You have been reading a chapter from
Python GUI Programming Cookbook
Published in: Dec 2015
Publisher:
ISBN-13: 9781785283758
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 €18.99/month. Cancel anytime