Lecture 38

GUIs with tkinter 2

MCS 260 Fall 2020
David Dumas

Reminders

  • Quiz 13 available
  • TA office hours (open to all) replace discussions on Tue Nov 24. These use different zoom links—see announcement on course home page.

Goal

Build a tkinter GUI application to encode text by rotating each letter forward in the alphabet by a fixed number of places.

Subclassing Tk

A good way to make GUI applications is to subclass tkinter.Tk and put GUI setup code in __init__.

Then, application data can be stored as class attributes.

Commands and other callbacks can be methods.

rowspan and columnspan

These options for .grid of a widget make it span multiple columns or rows in the layout.

tkinter variables

tkinter offers mutable variable classes designed to work with widgets:

  • tkinter.StringVar — mutable string
  • tkinter.DoubleVar — mutable float
  • tkinter.IntVar — mutable integer

All use .set(val) to set, .get() to get. They automatically notify widgets that use them of changes.

Variable change callbacks

tkinter variables let us register a function to be called whenver the value changes:

tkvar.trace_add("write",callback)

The function callback is called with three arguments (internal name, internal index, operation). Usually you want to ignore all of these arguments.

Window title

Tk.title(s) sets the window title (in title bar).

References

Revision history

  • 2020-11-19 Initial publication