Python GUI Programming - A Complete Reference Guide
eBook - ePub

Python GUI Programming - A Complete Reference Guide

Develop responsive and powerful GUI applications with PyQt and Tkinter

  1. 746 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Python GUI Programming - A Complete Reference Guide

Develop responsive and powerful GUI applications with PyQt and Tkinter

About this book

Explore Python's GUI frameworks and create visually stunning and feature-rich applications

Key Features

  • Integrate stunning data visualizations using Tkinter Canvas and Matplotlib
  • Understand the basics of 2D and 3D animation in GUI applications
  • Explore PyQt's powerful features to easily design and customize your GUI applications

Book Description

A responsive graphical user interface (GUI) helps you interact with your application, improves user experience, and enhances the efficiency of your applications. With Python, you'll have access to elaborate GUI frameworks that you can use to build interactive GUIs that stand apart from the rest.

This Learning Path begins by introducing you to Tkinter and PyQt, before guiding you through the application development process. As you expand your GUI by adding more widgets, you'll work with networks, databases, and graphical libraries that enhance its functionality. You'll also learn how to connect to external databases and network resources, test your code, and maximize performance using asynchronous programming. In later chapters, you'll understand how to use the cross-platform features of Tkinter and Qt5 to maintain compatibility across platforms. You'll be able to mimic the platform-native look and feel, and build executables for deployment across popular computing platforms.

By the end of this Learning Path, you'll have the skills and confidence to design and build high-end GUI applications that can solve real-world problems.

This Learning Path includes content from the following Packt products:

  • Python GUI Programming with Tkinter by Alan D. Moore
  • Qt5 Python GUI Programming Cookbook by B. M. Harwani

What you will learn

  • Visualize graphs in real time with Tkinter's animation capabilities
  • Use PostgreSQL authentication to ensure data security for your application
  • Write unit tests to avoid regression when updating code
  • Handle different signals generated on mouse clicks using QSpinBox and sliders
  • Employ network concepts, internet browsing, and Google Maps in UI
  • Use graphics rendering to implement animations in your GUI

Who this book is for

If you're an intermediate Python programmer looking to enhance your coding skills by writing powerful GUIs in Python using PyQT and Tkinter, this is an ideal Learning Path for you. A strong understanding of the Python language is a must to grasp the concepts explained in this book.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Event Handling - Signals and Slots

In this chapter, we will learn about the following topics:
  • Using Signal/Slot Editor
  • Copying and pasting text from one Line Edit widget to another
  • Converting data types and making a small calculator
  • Using the Spin Box widget
  • Using scrollbars and sliders
  • Using List Widget
  • Selecting multiple list items from one List Widget and displaying them in another
  • Adding items into List Widget
  • Performing operations in List Widget
  • Using the Combo Box widget
  • Using the Font Combo Box widget
  • Using the Progress Bar widget

Introduction

Event handling is an important mechanism in every application. The application should not only recognize the event, but must take the respective action to serve the event, too. The action taken on any event determines the course of the application. Each programming language has a different technique for handling or listening to events. Let's see how Python handles its events.

Using Signal/Slot Editor

In PyQt, the event handling mechanism is also known as signals and slots. An event can be in the form of clicking or double-clicking on a widget, or pressing the Enter key, or selecting an option from a radio button, checkbox, and so on. Every widget emits a signal when any event is applied on it and, that signal needs to be connected to a method, also known as a slot. A slot refers to the method containing the code that you want to be executed on the occurrence of a signal. Most widgets have predefined slots; you don't have to write code to connect a predefined signal to a predefined slot.
You can even edit a signal/slot by navigating to the Edit | Edit Signals/Slots tool in the toolbar.

How to do it...

To edit the signals and slots of different widgets placed on the form, you need to switch to signals and slots editing mode by performing the following steps:
  1. You can press the F4 key, navigate to the Edit | Edit Signals/Slots option, or select the Edit Signals/Slots icon from the toolbar. The mode displays all the signal and slot connections in the form of arrows, indicating the connection of a widget with its respective slot.
You can also create new signal and slot connections between widgets in this mode and delete an existing signal.
  1. To establish a signal and slot connection between two widgets in a form, select a widget by left-clicking the mouse on the widget, dragging the mouse towards another widget to which you want to connect, and releasing the mouse button over it.
  2. To cancel the connection while dragging the mouse, simply press the Esc key.
  3. On releasing the mouse over the destination widget, a Connection Dialog box appears, prompting you to select a signal from the source widget and a slot from the destination widget.
  4. After selecting the respective signal and slot, select OK to establish the signal and slot connection.
The following screenshot shows dragging a Push Button over a Line Edit widget:
  1. On releasing the mouse button on the Line Edit widget, you get the list of predefined signals and slots, as shown in the following screenshot:
You can also select Cancel in the Configure Connection dialog box to cancel the signal and slot connection.
  1. When connected, the selected signal and slot will appear as labels in the arrow, connecting the two widgets.
  2. To modify a signal and slot connection, double-click the connection path or one of its labels to display the Configure Connection dialog box.
  3. From the Configure Connection dialog, you can edit a signal or a slot as desired.
  4. To delete a signal and slot connection, select its arrow on the form and press the Delete key.
The signal and slot connection can also be established between any widget and the form. To do so, you can perform the following steps:
  1. Select the widget, drag the mouse, and release the mouse button over the form. The end point of the connection changes to the electrical ground symbol, representing that a connection has been established with the form.
  2. To come out of signal and slot editing mode, navigate to Edit | Edit Widgets or press the F3 key.

Copying and pasting text from one Line Edit widget to another

This recipe will make you understand how an event performed on one widget invokes a predefined action on the associated widget. Because we want to copy content from one Line Edit widget on clicking the push button, we need to invoke the selectAll() method on the occurrence of the pressed() event on push button. Also, we need to invoke the copy() method on occurrence of the released() event on the push button. To paste the content in the clipboard into another Line Edit widget on clicking of another push button, we need to invoke the paste() method on the occurrence of the clicked() event on another push button.

Getting ready

Let's create an application that consists of two Line Edit and two Push Button widgets. On clicking the first push button, the text in the first Line Edit widget will be copied and on clicking the second push button, the text copied from the first Line Edit widget will be pasted onto the second Line Edit widget.
Let's create a new application based on the Dialog without Buttons template by performing the following steps:
  1. Begin by adding QLineEdit and QPushButton to the form by dragging and dropping the Line Edit and Push Button widgets from the Widget box on the form.
To preview a form while editing, select either Form, Preview, or use Ctrl + R .
  1. To copy the text of the Line Edit widget when the user selects the push button on the form, you need to connect the push button's signal to the slot of Line Edit. Let's learn how to do it.

How to do it...

Initially, the form is in widget editing mode, and to apply signal and slot connections, you need to first switch to signals and slots editing mode:
  1. Select the Edit Signals/Slots icon from the toolbar to switch to signals and slots editing mode.
  1. On the form, select the push button, drag the mouse to the Line Edit widget, and release the mouse button. The Con...

Table of contents

  1. Title Page
  2. Copyright
  3. About Packt
  4. Contributors
  5. Preface
  6. Introduction to Tkinter
  7. Designing GUI Applications with Tkinter
  8. Creating Basic Forms with Tkinter and ttk Widgets
  9. Reducing User Error with Validation and Automation
  10. Planning for the Expansion of Our Application
  11. Creating Menus with Menu and Tkinter Dialogs
  12. Navigating Records with Treeview
  13. Improving the Look with Styles and Themes
  14. Creating Automated Tests with unittest
  15. Improving Data Storage with SQL
  16. Connecting to the Cloud
  17. Visualizing Data Using the Canvas Widget
  18. Creating a User Interface with Qt Components
  19. Event Handling - Signals and Slots
  20. Understanding OOP Concepts
  21. Understanding Dialogs
  22. Understanding Layouts
  23. Networking and Managing Large Documents
  24. Database Handling
  25. Using Graphics
  26. Implementing Animation
  27. Using Google Maps
  28. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Python GUI Programming - A Complete Reference Guide by Alan D. Moore, B.M. Harwani in PDF and/or ePUB format, as well as other popular books in Computer Science & Desktop Applications. We have over one million books available in our catalogue for you to explore.