Python, Tkinter GUI Practical Examples Tutorial

Comments are closed

Tkinter is a simple to use, cross-platform(Linux/Windows/OSx) GUI program – the only built-in GUI packaged with Python. It is designed on Tcl/Tk -a popular open-source GUI scripting language for a wide range of programming languages ( c, C++, Perl, PHP, Ruby etc.).

Here’s how to get started with Tkinter:

Example 1: Introducing Root Window

rootTkinter uses the term ‘root window” for a graphical window which accommodates all the widgets. The first step in Tkinter GUI designing is to create a root window. To create a new root window:

from Tkinter import *
root = Tk(className="My first GUI") # creates root window
# all components of thw window will come here
root.mainloop() # To keep GUI window running

Not very exciting, right ? Just a blank frame. Lets now add some elements to the root window.

Example 2. Adding Widgets

Widget refers to components that we add to the root window.

hw

Example 2: Hello World

This includes components like buttons, label, text area, menu, check button, entry, canvas, sliders and other elements that are added to the root window.

The syntax for adding a widget is :

Widget-name (configuration options for the widget)

In this example, we will add a label:

from Tkinter import *
root = Tk(className ="My first GUI") #add a root window named Myfirst GUI
foo = Label(root,text="Hello World") # add a label to root window
foo.pack()
root.mainloop()

To-do exercise::

Exercise 1) Try to configure the widget with various options like: bg=”red”, family=”times”, size=18

Exercise 2) Experiment with other widget types:
Try to change the widget type and configuration options to experiment with other widget types like Message, Button, Entry, Checkbutton, Radiobutton, Scale etc.

Configuration options for the common widget sets in Tkinter are as follows:

foo = Label(root,text="Some Label text here") # add a label to root window
foo = Message(root,text=" Some Long text here")# used instead of label, when the text is long
foo = Button(root,text="Login",command= function_when_pressed) # button - discussed in next example
foo = Entry(root,textvariable=svalue) # adds a textarea widget
foo = Checkbutton(root,text="Tick Me", variable=ticked_yes) # tickbutton
foo = Radiobutton(root,text="Option1",variable=opt1, value="option1") #radiobutton
foo = Scale(root, label="Volume Control",variable=volume, from =0, to=10, tickinterval=2, orient='vertical', command=on_slide) # slider

Example 3.  Introducing & Handling Events

In the previous example, we added a label. A label is a static widget with no associated actionable elements.

button

Example 3: Clicking the button invokes the custom defined function ‘act’

Compare this to widgets like button or text area – which have an associated event. For example clicking on the button should result in some event.

In Tkinter, we first define a function which will be invoked on happening of an event.(on click of button in our example)

from Tkinter import Button
def act(): # defines an event function - for click of button
    print "I-M-pressed"
foo = Button(None,text="Press Me",command=act) # create & configure widget 'button"
foo.pack() # defines placement & geometry of the widget (will use it later)
foo.mainloop() # an event loop to invoke custom function "act"

Example 3 introduces all the vital steps that go into a typical Tkinter based GUI program.entry

Example 4  Working with TextField (Entry Widget)

Last in the series of examples, we use the TextField called the Entry Widget in Tkinter/Python. We define a simple function which will print the data entered by the user in the text area, upon clicking of a button.

 

from Tkinter import *
root = Tk(className ="My first GUI")
svalue = StringVar() # defines the widget state as string
w = Entry(root,textvariable=svalue) # adds a textarea widget
w.pack()
def act():
    print "you entered"
    print '%s' % svalue.get()
foo = Button(root,text="Press Me", command=act)
foo.pack()
root.mainloop()

Python-Tkinter GUI Programming in a Nut shell

In a Tkinter program you will almost always:

  • Start by making a root window
  • Create and configure Widgets within the root window
  • Decide Placement of Widgets: with the command foo.pack(). We disucss this in detail later.
  • Create Event handling functions: that would respond to any GUI events (like the event “button click”)
  • Run an event loop: to invoke event functions

This tutorial introduced the various functional elements of a Python & GUI programming with Tkinter.

What was not covered:

We did not cover how to place the various widgets at desired locations in the root window frame. This is done with the pack() command. We discuss that in the next article.