Tkinter Graphics Programming

Tkinter Graphics Programming – Graphical User Interface (GUI) Terminology

Graphical User Interface (GUI) terminology specific to Tkinter Graphics Programming is described. Here, we introduce the concept of widgets and GUI terminology.

Widget

Widget: The term ‘widget’ is used in graphical user interface and denotes things like windows, buttons, pulldown menus, checkboxes, etc.  Widgets are graphical user interface components. These are the graphical objects that are used to allow the user to interact with the program.

In the Object Oriented Programming chapter, you learned about class and objects. Widgets are objects as well. This means widgets are instances of classes that represent labels, buttons, frames, check boxes, entry boxes, and so on.

More information on Tkinter Widgets is at the link below:

<a href=”https://www.scripps.edu/sanner/python/inputform/tkinterWidgets.html“>

Tkinter Graphics Programming Terms Used in Programming

Below, we define some of the terms used in Tkinter Graphics Programming.

Container 

The container is just a container into which we can put widgets. One example of a container is a canvas’, which is used to draw lines or curves.  Another example of a container is Frame. A ‘Frame’ is a class in Python, from which instances (objects) of frames can be created. You can put widgets in a ‘Frame’.

Frame

As mentioned above, the frame is a container into which we can put widgets. ‘Frame’ is a tkinter class from which instances (objects) can be created. As an example, the statement

frame1 = Frame(root) 

creates an instance of the ‘Frameclass. This statement names this instance as ‘frame1’.  In addition, this statement establishes a parent/child relationship between ‘root’ as the parent and ‘frame1’ as the child.  ‘frame1’ is now a child of the parent ‘root’. See below for the parent/child relationship.

Events

The GUI programs are continuously running (until terminated by the user).  While the GUI program is running, it monitors for external inputs, like the click of the mouse, or a key press of the keyboard like ENTER, or UP, DOWN arrow keys. These inputs are called events.  GUI program will take some action as a result of the mouse click or key press. As an example, the event may be a mouse click action that will result in changing the color of the text when the mouse clicks a button widget.

Event Handlers 

The part of the program that takes actions in response to the events (mouse clicks or key press) is called Event Handlers. The event handler is a method that operates on the object (widget).

Bind 

For the widget to show the change (such as changing the color of the widget), we need to associate Event Handler to the widget and the event.  The process of association of the widget with the Event Handler and the Event is called binding.  The binding is done through a method called bind(). The process of binding associates the following three different things:

  • Event type (key press, mouse click, etc.)
  • Widget (button, check boxes, etc.)
  • Event-handler routine. 

Root (Parent)

The frame (container) that contains other widgets is called the ‘parent’. Root is the top-level container.

Child

The widget that is placed in the container is called the ‘child’ of the container.  The container and widgets are said to have a parent/child relationship.

Attributes

All widgets have several attributes. Some of the widget’s attributes are such as:

  • background color
  • foreground color
  • size
  • font type
  • font face (italics, bold, etc.)
  • the size of the text written on top of the widget
  • and many more

These attributes are selected by using keyword parameters when the widget instance is created.

Examples of parameters are:

  • ‘text’,
  • ‘foreground’
  • ‘background’
  • etc.

Geometry Manager 

There are three geometry managers in Tkinter – pack, grid, and place.

  • grid – Arranges widgets in a grid, like a graph paper with horizontal and vertical lines
  • pack – Packs widgets into a cavity
  • place – Positions widgets at absolute locations

The Geometry Managers are used to set the position of the widget within the container. A geometry manager tells Tkinter how to show the containers and widgets on the screen. On this website, we will be using only the ‘Pack’ method and ‘grid’ in the program examples later in this chapter.

pack() method

Pack” invokes the Tkinter “pack” Geometry Manager. The pack() method tells the widget to size itself (make it bigger or smaller) to fit the text/image written on the widget. When you use the pack() method on a widget, in addition to sizing itself to fit the text, it makes the widget visible on the screen. If you do not use the pack() method, the widget will not be visible.

pack() method has an option called ‘side’ that tells Tkinter how to position the widgets in the container.  The pack() method’s ‘side’ option arguments are ‘LEFT’, ‘RIGHT’, ‘TOP’, and ‘BOTTOM’.  The ‘side’ options give us the capability of justifying the widget to the left, right, top, or bottom of the container. We will use the ‘side’ option in a later program. 

Mainloop()

The mainloop (also called event loop) continuously watches all events that happen while the program is running such as key presses, mouse clicks, etc. Since events and event handlers are bound to each other, when an event occurs, the event loop calls the event handler and lets it know that an event has occurred. If there are no events, the event loop runs in an infinite loop.

More information on Graphical User Interface and Tkinter Graphics Programming is at the link below:

<a href=”https://docs.python.org/3/library/tk.html“>

On the next page, we will see how we can use widgets to do Graphical User Interface programming.

Copyright © 2019 – 2021 softwareprogramming4kids.com

All Rights Reserved

Verified by MonsterInsights