Intro — What We’re Doing
Some terminology and site conventions may need a bit of explaining.
A Bit on Script File Features
This is a quick intro to what you’ll find in the demo scripts used on this blog, but before we go there, let’s start with some terminology…
Parent or Parent?
One of the things that trips up those new to a GUI toolkit that allows OOP-style code is how the term parent is used. And with two specific meanings, there are two definitions:
- a parent is a widget containing another widget, and
- a parent is also the class from which another class is derived.
To avoid this confusion, I’ll refer to various objects, widgets, and classes thusly:
- a widget that contains another widget is a parent,
- a widget contained by another widget is a child,
- a class from which another class is derived, is the super-class, and
- a class derived from another class is the derived class.
If you take a look at the screenshot above, you’ll see that each class definition I write has comments that appear in almost every one of them. Here’s an explanation:
|# object attributes||Variables specific to a class and all objects instantiated from it|
|# configure||Statements configuring various class/object attributes|
|# populate||If the widget object contains child widgets, they go here|
|# layout||A convenience for complex
Layout – Just to Clarify Further
If a child widget is the only child of a parent widget, the call to
grid() will be made from within the child’s
But if there are lots of widgets—a complex layout—it’s easier to tweak the layout if all the
grid() calls are made from the parent. Even after the layout is tweaked to within an inch of its life, it’s probably best to leave these
grid() statements in the parent in case a later code revision sees you adding another child or two.
The grid() Method
And speaking of… you may ask: what exactly is the
grid() method when it’s at home?
The short answer is: it’s one way to get a widget to show up in a GUI layout. There are three methods for doing this and they are:
place()— put a widget at exact coordinates within the
pack()— stuff the widget into virtual boxes, either horizontal or vertical, and
grid()— use virtual rows and columns… in short, a table.
Most of the time,
grid() is all we need, so unless it’s absolutely necessary to use one of the others, that’s what we’ll stick with.
self identifier is used when a class needs to refer to—big surprise—itself. Some languages use
this, but Python uses
self. Giving objects a way to identify themselves to themselves is a fundamental part of OOP.
Attribute is just a fancy way of saying variable. The difference is that attributes are only found inside classes whereas variables can be in any scope within your code. Suffice to say, there won’t be any variables in any of these demos… probably.
Attributes are defined within the
__init__() method (in the
#object attribute section, in fact) and use the
self prefix—as mentioned earlier—so they can be accessed from anywhere within the instantiated object. Here are a couple of examples:
self.width = 100 self.height = 200 self.geometry = "100x200"
Defining a Child Widget
This is the other common use of
self. Whenever a child widget is defined, the parent has to identify itself to the child like this:
self.child_widget = ChildWidget(self)
Within the child’s
__init__ method definition, the parent is identified this:
def __init__(self, parent):
Then, when the child calls
self.grid(), nothing more needs to be done. tkinter has enough information to figure out the parent/child relationship.
But all this is academic. Next time, we’ll start to see these things in action.
Please feel free to accept this invitation to become our newest sponsor.
And have a great day!
Comments? Questions? Observations?
Did we miss a tidbit of information that would make this post even more informative? Let's talk about it in the comments.
- You can also click the link below to email us,
- follow us on the Tkooper Facebook page, or
- You can subscribe via RSS so you won't miss anything.
Thank you very much for dropping by!
© Copyright 2021 Ronald V. Tarrant