Getting started with tkinter
Remarks#
Tkinter (”Tk Interface”)is python’s standard cross-platform package for creating graphical user interfaces (GUIs). It provides access to an underlying Tcl interpreter with the Tk toolkit, which itself is a cross-platform, multilanguage graphical user interface library.
Tkinter isn’t the only GUI library for python, but it is the one that comes standard. Additional GUI libraries that can be used with python include wxPython, PyQt, and kivy.
Tkinter’s greatest strength is its ubiquity and simplicity. It works out of the box on most platforms (linux, OSX, Windows), and comes complete with a wide range of widgets necessary for most common tasks (buttons, labels, drawing canvas, multiline text, etc).
As a learning tool, tkinter has some features that are unique among GUI toolkits, such as named fonts, bind tags, and variable tracing.
Differences between python 2 and 3
Tkinter is largely unchanged between python 2 and python 3, with the major difference being that the tkinter package and modules were renamed.
Importing in python 2.x
In python 2.x, the tkinter package is named Tkinter
, and related packages have their own names. For example, the following shows a typical set of import statements for python 2.x:
import Tkinter as tk
import tkFileDialog as filedialog
import ttk
Importing in python 3.x
Although functionality did not change much between python 2 and 3, the names of all of the tkinter modules have changed. The following is a typical set of import statements for python 3.x:
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
Further Reading
Versions#
Tcl
Version | Release Date |
---|---|
8.6 | 2016-07-27 |
8.5 | 2016-02-12 |
8.4 | 2013-06-01 |
8.3 | 2002-10-18 |
8.2 | 1999-12-16 |
8.1 | 1999-05-26 |
8.0 | 1999-03-09 |
Python
Version | Release Date |
---|---|
3.6 | 2016-12-23 |
3.5 | 2015-09-13 |
3.4 | 2014-03-17 |
3.3 | 2012-09-29 |
3.2 | 2011-02-20 |
3.1 | 2009-06-26 |
3.0 | 2008-12-03 |
2.7 | 2010-07-03 |
2.6 | 2008-10-02 |
2.5 | 2006-09-19 |
2.4 | 2004-11-30 |
2.3 | 2003-07-29 |
2.2 | 2001-12-21 |
2.1 | 2001-04-15 |
2.0 | 2000-10-16 |
Installation or Setup
Tkinter comes pre-installed with the Python installer binaries for Mac OS X and the Windows platform. So if you install Python from the official binaries for Mac OS X or Windows platform, you are good to go with Tkinter.
For Debian versions of Linux you have to install it manually by using the following commands.
For Python 3
sudo apt-get install python3-tk
For Python 2.7
sudo apt-get install python-tk
Linux distros with yum installer can install tkinter module using the command:
yum install tkinter
Verifying Installation
To verify if you have successfully installed Tkinter, open your Python console and type the following command:
import tkinter as tk # for Python 3 version
or
import Tkinter as tk # for Python 2.x version
You have successfully installed Tkinter, if the above command executes without an error.
To check the Tkinter version, type the following commands in your Python REPL:
For python 3.X
import tkinter as tk
tk._test()
For python 2.X
import Tkinter as tk
tk._test()
Note: Importing Tkinter as tk
is not required but is good practice as it helps keep things consistent between version.
Hello, World! (minimal)
Let’s test our basic knowledge of tkinter by creating the classic “Hello, World!” program.
First, we must import tkinter, this will vary based on version (see remarks section about “Differences between Python 2 and 3”)
In Python 3 the module tkinter
has a lowercase t:
import tkinter as tk
In Python 2 the module Tkinter
has a uppercase T:
import Tkinter as tk
Using as tk
isn’t strictly necessary but we will use it so the rest of this example will work the same for both version.
now that we have the tkinter module imported we can create the root of our application using the Tk
class:
root = tk.Tk()
This will act as the window for our application. (note that additional windows should be Toplevel
instances instead)
Now that we have a window, let’s add text to it with a Label
label = tk.Label(root, text="Hello World!") # Create a text label
label.pack(padx=20, pady=20) # Pack it into the window
Once the application is ready we can start it (enter the main event loop) with the mainloop
method
root.mainloop()
This will open and run the application until it is stopped by the window being closed or calling exiting functions from callbacks (discussed later) such as root.destroy()
.
Putting it all together:
import tkinter as tk # Python 3.x Version
#import Tkinter as tk # Python 2.x Version
root = tk.Tk()
label = tk.Label(root, text="Hello World!") # Create a text label
label.pack(padx=20, pady=20) # Pack it into the window
root.mainloop()
And something like this should pop up:
Hello, World! (modular, object-oriented)
import tkinter as tk
class HelloWorld(tk.Frame):
def __init__(self, parent):
super(HelloWorld, self).__init__(parent)
self.label = tk.Label(self, text="Hello, World!")
self.label.pack(padx=20, pady=20)
if __name__ == "__main__":
root = tk.Tk()
main = HelloWorld(root)
main.pack(fill="both", expand=True)
root.mainloop()
Note: It’s possible to inherit from just about any tkinter widget, including
the root window. Inheriting from tkinter.Frame
is at least arguably
the most flexible in that it supports multiple document interfaces (MDI),
single document interfaces (SDI), single page applications, and multiple-page
applications.