Book Image

Python GUI Programming - A Complete Reference Guide

By : Alan D. Moore, B. M. Harwani
Book Image

Python GUI Programming - A Complete Reference Guide

By: Alan D. Moore, B. M. Harwani

Overview of this book

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
Table of Contents (28 chapters)
Title Page

Introducing IDLE

IDLE is an integrated development environment that is bundled with the Windows and macOS Python distributions (it's readily available in most Linux distributions as well, usually as IDLE or IDLE3). IDLE is written in Python using Tkinter, and it provides us with not only an editing environment for Python, but also a great example of Tkinter in action. So, while IDLE's rudimentary feature set may not be considered professional grade by many Python coders, and while you may already have a preferred environment for writing Python code, I encourage you to spend some time using IDLE as you go through this book.

Let's get familiar with IDLE's two primary modes: shell mode and editor mode.

Using the shell mode of IDLE

When you launch IDLE, you begin in shell mode, which is simply a Python Read-Evaluate-Print-Loop (REPL) similar to what you get when you type python in a terminal window.

Take a look at the shell mode in the following screenshot:

IDLE's shell has some nice features that you don't get from the command-line REPL, like syntax highlighting and tab-completion. The REPL is essential to the Python development process, as it gives you the ability to test code in real time and inspect classes and APIs without having to write complete scripts. We'll use the shell mode in later chapters to explore the features and behaviors of modules. If you don't have a shell window open, you can open one by clicking on Start, then selecting Run, and searching for Python shell.

Using the editor mode of IDLE

Editor mode is for creating Python script files, which you can later run. When the book tells you to create a new file, this is the mode you'll use. To open a new file in the editor mode, simply navigate to File | New File in the menu or hit Ctrl + N on the keyboard. 

The following is a window where you can start typing a script:

You can run your script without leaving IDLE by hitting F5 in the editor mode; the output will show up in a shell window.

IDLE as a Tkinter example

Before we start coding with Tkinter, let's take a quick look at what you can do with it by inspecting some of IDLE's UI. Navigate to Options | Configure IDLE from the main menu to open IDLE's configuration settings, where you can change IDLE's fonts, colors and theme, keyboard shortcuts, and default behaviors, as shown in the following screenshot:

Consider some of the following components that make up this user interface:

  • There are drop-down lists and radio buttons that allow you to select between different options
  • There are many push buttons that you can click on to execute actions
  • There is a text window that can display multi-colored text
  • There are labeled frames that contain groups of components

Each of these components is known as a widget; we're going to meet these widgets and more throughout this book and learn how to use them as they've been used here. We'll begin, however, with something much simpler.