Recap part 2 – Tkinter


Well it certainly looks as though we’re in for a splendid afternoon’s sport in this the 127th Upperclass Twit of the Year Show. Well the competitors will be off in a moment so let me just identify for you. (close-up of the competitors) Vivian Smith-Smythe-Smith has an O-level in chemo-hygiene. Simon-Zinc-Trumpet-Harris, married to a very attractive table lamp.

In the last lesson, I gave you a quick refresher on classes.  In this tute, we’re going to remember what Tkinter is.  Technically, Tkinter is something that allows Python programs to use something called Tk.   According to its website, Tk is “a graphical user interface toolkit that takes developing desktop applications to a higher level than conventional approaches. Tk is the standard GUI not only for Tcl, but for many other dynamic languages, and can produce rich, native applications that run unchanged across Windows, Mac OS X, Linux and more.

In other words, Tkinter is what allows your Python programs to interact with a user through a windowing system, rather than the command line I had been forcing you to use earlier.  Tkinter is not unique, there are other toolkits that Python can use to do exactly the same thing. We are working with Tkinter here because it’s the de facto standard for Python – also because you should have Tkinter (they come together).  Tkinter’s look is pretty ugly, but we are here for practicality, not beauty.  You can research other toolkits on line if you want to have a different look for your application, but you will need to learn how Python calls those other toolkits, as it will be different.  Other toolkits may also provide additional widgets.   That said, there are a number of common elements to any GUI system that you use:

  • GUIs interact with users through areas on the user’s screen called windows;
  • windows typically have an area (such as a top and bottom bar) which are reserved for operating system related functions – such as the window’s title,  minimize, maximize, close etc.  with the balance of the space being available for use by the program;

  • the GUI uses a geometry manager to arrange widgets within the window;
  • different geometry managers can be used depending on how you want your widgets arranged.  In our examples to date we have not specified a geometry manager, so Tkinter has used a default geometry manager;
  • widgets are graphical components which are displayed in the window.  So far we have met buttons and labels, but there are many more.
  • each time a user does something in the window, an event is generated.
  • there are a whole host of things that can generate an event.  These include: moving the mouse pointer, pressing a button on the mouse, releasing a button on the mouse, pressing a key on the keyboard, releasing a key on the keyboard etc.  So, for example, if you move the mouse over the window of your browser and click on this text, your operating system will generate at least two events – an event for when you press down on the mouse and an event for when you take your finger off the mouse.  In reality many more events will be generated;
  • an event is a special type of object which Tkinter uses.  The event object has a number of attributes.  In particular, the event object records the type of event (eg mouse click vs keyboard press) and the location in the window where that event occurred;
  • when Tkinter receives an event it checks to see whether you have told it to use a handler for that event.  If so, it will pass the event to the handler, if not it will ignore the event;
  • an event handler is a function (or a method) which does something when the event occurs, and may receive the event as a parameter.  An example of a handler is the nextImage function in this tutorial;
  • the act of telling Tkinter to use a given handler for a given event is called binding the event to the handler.  In our examples we have bound the handler by setting the ‘command’ key of the relevant widget but there is a separate function called bind();
  • in order to become visible in a Window every widget it needs to be pack() ed.   If you ever seem to be missing a widget, make sure that widget has been pack()ed (and, if the widget is inside another widget that all widgets up the chain are pack()ed)
  • Tkinter is imported with the code: 

from Tkinter import *

This code imports everything from the Tkinter namespace into the program’s namespace.  This is normally bad practice, but Tkinter is an exception.

In the tutorials to follow we will put both Tkinter and our knowledge of classes to some use – what’s more, in a vaguely practical way (if you think minecraft is practical that is…)

 

About these ads

3 Responses to Recap part 2 – Tkinter

  1. Pingback: Linux News » Python4Kids New Tutorial: Recap Part 2 – Tkinter

  2. Pingback: Python 4 Kids: Recap part 2 – Tkinter | Python | Syngu

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 67 other followers

%d bloggers like this: