Composing widgets to display information
It is difficult to build large applications if all the code is contained in a single class. By splitting the GUI code into specific classes, we can modularize the structure of our program and create widgets with well-defined purposes.
Getting ready
Apart from importing the Tkinter package, we will import the Contact
class from the preceding recipe:
import tkinter as tk import tkinter.messagebox as mb from chapter5_01 import Contact
Verify that the chapter5_01.py
file is in the same directory; otherwise, this import-from
statement will raise ImportError
.
How to do it...
We will create a scrollable list that will show all contacts. To represent each item in the list as a string, we will display the contact's last and first names:
class ContactList(tk.Frame): def __init__(self, master, **kwargs): super().__init__(master) self.lb = tk.Listbox(self, **kwargs) scroll = tk.Scrollbar(self, command=self.lb.yview) self.lb.config...