tkinter

Topics related to tkinter:

Getting started with tkinter

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

The Tkinter Entry Widget

These examples assume that tkinter has been imported with either import tkinter as tk (python 3) or import Tkinter as tk (python 2).

The Tkinter Radiobutton widget

These examples assume that tkinter has been imported with either import tkinter as tk (python 3) or import Tkinter as tk (python 2).

Reference:

enter image description here

To turn the above example into a “button box” rather than a set of radio buttons, set the indicatoron option to 0. In this case, there’s no separate radio button indicator, and the selected button is drawn as SUNKEN instead of RAISED:

enter image description here

-effbot

Multiple windows (TopLevel widgets)

Delaying a function

Syntax assumes a widget accepted by the method .after has been previously created (i.e widget=tk.Label(parent))

Scrolling widgets

These examples assume that tkinter has been imported with either import tkinter as tk (python 3) or import Tkinter as tk (python 2).

Tkinter Geometry Managers

Adding Images To Label/Button

Ttk widgets

These examples assume that tkinter has been imported with either import tkinter as tk (python 3) or import Tkinter as tk (python 2).

It is also assumed that ttk has been imported with either from tkinter import ttk (python 3) or import ttk (python 2).

Customize ttk styles