Book Image

Python GUI Programming Cookbook - Third Edition

By : Burkhard Meier
Book Image

Python GUI Programming Cookbook - Third Edition

By: Burkhard Meier

Overview of this book

Python is a multi-domain, interpreted programming language that is easy to learn and implement. With its wide support for frameworks to develop GUIs, you can build interactive and beautiful GUI-based applications easily using Python. This third edition of Python GUI Programming Cookbook follows a task-based approach to help you create effective GUIs with the smallest amount of code. Every recipe in this book builds upon the last to create an entire, real-life GUI application. These recipes also help you solve problems that you might encounter while developing GUIs. This book mainly focuses on using Python’s built-in tkinter GUI framework. You'll learn how to create GUIs in Python using simple programming styles and object-oriented programming (OOP). As you add more widgets and expand your GUI, you will learn how to connect to networks, databases, and graphical libraries that greatly enhance the functionality of your GUI. You’ll also learn how to use threading to ensure that your GUI doesn't become unresponsive. Toward the end, you’ll learn about the versatile PyQt GUI framework, which comes along with its own visual editor that allows you to design GUIs using drag and drop features. By the end of the book, you’ll be an expert in designing Python GUIs and be able to develop a variety of GUI applications with ease.
Table of Contents (13 chapters)

Using scrolled text widgets

ScrolledText widgets are much larger than simple Entry widgets and span multiple lines. They are widgets like Notepad and wrap lines, automatically enabling vertical scroll bars when the text gets larger than the height of the ScrolledText widget.

Getting ready

How to do it...

By adding the following lines of code, we create a ScrolledText widget:

  1. Start with the GUI_radiobutton_widget.py module and save it as GUI_scrolledtext_widget.py.

  1. Import scrolledtext:
from tkinter import scrolledtext
  1. Define variables for the width and height:
scrol_w = 30
scrol_h = 3
  1. Create a ScrolledText widget:
scr = scrolledtext.ScrolledText(win, width=scrol_w, height=scrol_h, wrap=tk.WORD)
  1. Position the widget:
scr.grid(column=0, columnspan=3)

The preceding steps will finally produce the following code (GUI_scrolledtext_widget.py):

  1. Run the code. We can actually type into our widget, and if we type enough words, the lines will automatically wraparound:

Once we type in more words than the height of the widget can display, the vertical scroll bar becomes enabled. This all works out of the box without us needing to write any more code to achieve this:

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

How it works...

In line 11, we import the module that contains the ScrolledText widget class. Add this to the top of the module, just below the other two import statements.

Lines 100 and 101 define the width and height of the ScrolledText widget we are about to create. These are hardcoded values we are passing into the ScrolledText widget constructor in line 102.

These values are magic numbers found by experimentation to work well. You might experiment by changing scol_w from 30 to 50 and observe the effect!

In line 102, we are also setting a property on the widget by passing in wrap=tk.WORD. By setting the wrap property to tk.WORD, we are telling the ScrolledText widget to break lines by words so that we do not wraparound within a word. The default option is tk.CHAR, which wraps any character regardless of whether we are in the middle of a word.

The second screenshot shows that the vertical scroll bar moved down because we are reading a longer text that does not entirely fit into the x, y dimensions of the ScrolledText control we created.

Setting the columnspan attribute of the grid layout to 3 for the ScrolledText widget makes this widget span all of the three columns. If we do not set this attribute, our ScrolledText widget would only reside in column one, which is not what we want.

We've successfully learned how to use scrolled text widgets. Now, let's move on to the next recipe.