Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
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 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. Building GUIs with PyQt5 11. Best Practices 12. Other Books You May Enjoy

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.

You have been reading a chapter from
Python GUI Programming Cookbook - Third Edition
Published in: Oct 2019
Publisher:
ISBN-13: 9781838827540
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
Banner background image