Basic tkinter usage for Python 3

I am offering a job online and was flooded with applicants. I needed a way to cut and paste the data and then save it for later processing.

Lessons in this Tutorial

  • tkinter basics
  • Classes
  • Basic flow control in loops
  • Python Dictionaries
  • List Splicing
  • json writes
  • Regex
  • Field validation
  • Grid layout in tkinter

Tkinter or tkinter?

Among the most popular gui frameworks for python such as Tkinter, wxWidgets, Qt, Gtk+, Kivy, FLTK, OpenGL Tkinter will prove to be the easiest to learn and license. Once you dig in a bit, you’ll see also that there are tutorials for both Tkinter and tkinter. The difference is Tkinter is for python 2 and tkinter is for python 3. Both are just a wrapper for Tk, a very old, but universal gui framework. I chose to learn and use tkinter because it’s easy enough to learn in an afternoon (specifically this is 1 afternoon of playing with tkinter).

tkinter Layout Systems

When you design your GUI you will need to pick either “pack” or “grid” layouts. I found that grid is easier and more precise. Pack tends to jumble things around, especially as you change resolutions. This tutorial will specifically demonstrate the grid layout system and ignore pack.

tkinter Grid Layout
Image 1990 table layout. Grid layout merges columns and rows just like old school HTML. I like it because it’s super simple and reliable.
tkinter Pack Layout
I didn’t find an advantage to pack except you don’t have to think much about the simple layouts. For some GUI apps, this might be plenty. You have about as much control as trying to organize a suitcase – it works great until you go through TSA and you have to reorganize it. I hate playing with pack, but not near as much as I hate TSA!

More about tkinter

Instead of rewriting clear, but dated docs on tkinter that will no doubt change by the time you read this, I’ll link to the python 3 tkinter docs. I’m sure you’ll find them as obfuscated as I did.

Form Parsing

Back to “why” I wrote this. I have an email form that collects data. It looks something like this in my email client:
Sample Form Data

I’ll be honest, I hate cut and paste anything. Moreover I hate typing things into a form. I needed to track their data though and process it after they applied for a job. I thought it would be a great exercise and save time and frustration to parse out the data I needed using python.

You’ll find that if you can easily manipulate data from spreadsheets, cut and paste, emails, and arbitrary inputs – people will think you are much smarter than you actually are. Form parsing (especially with a GUI), happens to be one of those magically creative things that people like.

Basic Steps to GUI Design

  • Decide on the Scope/Purpose
  • Sketch out the Inputs/Outputs
  • Consider ways it needs error checking
  • Code it and document as you go
  • Fix up your code as required

Anyway, that’s how you probably should do it. More often it looks like this:

  • Waste time coding a GUI first
  • spend extra time on the widgets of features you won’t ever use
  • Decide what you want the GUI to do
  • Rewrite it to account for the errors you didn’t think about
  • Pretend you know how to code
  • Go back after a few months and forget why you did things, then decide you should of documented it

For illustration purpose, assume I did the first way to create this script!

Error Checking

There isn’t much fantastic about the specific error checking I’m using, but it is a good illustration of how to validate form fields. (I didn’t need to validate these, but wanted to learn more about tkinter, so I added these tkinter field checks).

  • Name (must be longer than 5 chars)
  • Email (extremely basic regex, not a full email validator, just helps with typos)
  • PHone (must be 10 digits, strips and rebuilds to xxx-xxx-xxxx)

Knowing how to tie in a regex makes it easy enough to find other regex checks you might want for other apps (such as validating an IP address, network, port numbers, etc)

Just Gimme the Form Parser Script!

If the comments don’t help enough, feel free to ask questions. There are probably more “pythonic” ways of doing what I did below, but it works for my one afternoon of playing with tkinter.

Example of Usage

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.