This lesson introduces Python graphical user interfaces using Tkinter.

Objectives and Skills

edit

Objectives and skills for this lesson include:

  • Standard Library
    • Graphical User Interfaces

Readings

edit
  1. Wikipedia: Graphical user interface
  2. Wikipedia: Platform-independent GUI library
  3. Wikipedia: Tkinter

Multimedia

edit
  1. YouTube: TkInter Tutorial
  2. YouTube: Python tkinter Playlist

Examples

edit

The tkinter.Tk() Method

edit

The tkinter.Tk() method creates a toplevel widget of Tk, which usually is the main window of an application.[1]

import tkinter

root = tkinter.Tk()

root.mainloop()

The root.title() Method

edit

The root.title() method sets the window title.[2]

root.title("tkinter Example")

The root.maxsize() Method

edit

The maxsize() method returns the maximum size available for the window.[3]

root.maxsize()

The root.geometry() Method

edit

The geometry() method sets the size and offset in pixels for the window.[4]

root.geometry("%dx%d+0+0" % root.maxsize())

The tkinter.scrolledtext Object

edit

The tkinter.scrolledtext object implements a basic text widget with a vertical scroll bar.[5]

import tkinter.scrolledtext

scrolledtext = tkinter.scrolledtext.ScrolledText(root)

The pack() Method

edit

The pack() method packs widgets into the available window space.[6]

scrolledtext.pack(fill="both", expand=True)

The scrolledtext.insert() Method

edit

The scrolledtext.insert() method inserts text into the scrolled tex widget.

scrolledtext.insert(tkinter.INSERT, text)

tkinter scrolledtext Example

edit

The following example demonstrates using tkinter to display a scrolling text window.

import tkinter
import tkinter.scrolledtext

root = tkinter.Tk()
root.title("tkinter scrolledtext Example")
root.geometry("%dx%d+0+0" % root.maxsize())

scrolledtext = tkinter.scrolledtext.ScrolledText(root)
scrolledtext.pack(fill="both", expand=True)

text = \
    "Lorem ipsum dolor sit amet, consectetur adipiscibus elit, sed do " \
    "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim " \
    "ad minim veniam, quis nostrud exercitatio ullamco laboris nisi ut " \
    "aliquis ex ea commodo consequat. Duis aute irure dolor in " \
    "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla " \
    "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in " \
    "culpa qui officia deserunt mollit anim id est laborum.\n\n"

scrolledtext.insert(tkinter.INSERT, text)

root.mainloop()

tkinter canvas Example

edit

The following example demonstrates using tkinter to draw canvas shapes.[7]

import tkinter

root = tkinter.Tk()
root.title("tkinter Canvas Example")
root.geometry("%dx%d+0+0" % root.maxsize())

canvas = tkinter.Canvas(root)
canvas.pack(fill="both", expand=True)

canvas.create_line(50, 100, 150, 100, width=3, fill='darkred')
canvas.create_rectangle(200, 50, 300, 150, outline='gold', fill='gold')
canvas.create_oval(350, 50, 450, 150, outline='darkgreen', fill='darkgreen')
canvas.create_polygon(550, 50, 500, 150, 600, 150, outline='darkblue', fill='darkblue')

root.mainloop()

Activities

edit

Tutorials

edit
  1. Complete one or more of the following tutorials:

Practice

edit
  1. Modify one or more of the Python Programming/Files#Practice activities so that the input is passed from the command line and the output is displayed in a Tkinter scrolledtext window.
  2. Modify one or more of the Python Programming/Internet Data#Practice activities so that the input is passed from the command line and the output is displayed in a Tkinter scrolledtext window.
  3. Modify one or more of the Python Programming/Databases#Practice activities so that the input is passed from the command line and the output is displayed in a Tkinter scrolledtext window.

Lesson Summary

edit

GUI Concepts

edit
  • A graphical user interface (GUI) is type of user interface that allows users to interact with electronic devices through graphical icons and visual indicators such as secondary notation, instead of text-based user interfaces, typed command labels or text navigation.[8]
  • Designing the visual composition and temporal behavior of a GUI is an important part of software application programming in the area of human–computer interaction.[9]
  • The goal of human-computer interaction is to enhance the efficiency and ease of use for the underlying logical design of a stored program, a design discipline named usability.[10]
  • Typically, users interact with information by manipulating visual widgets that allow for interactions appropriate to the kind of data they hold.[11]
  • Large widgets, such as windows, usually provide a frame or container for the main presentation content such as a web page, email message or drawing. Smaller ones usually act as a user-input tool.[12]
  • The most common combination of widget elements in GUIs is the windows, icons, menus, pointer (WIMP) paradigm.[13]
  • A Platform Independent Graphical User Interface package is a software library that a programmer uses to produce GUI code for multiple computer platforms.[14]
  • Most platform-independent Graphical User Interface packages slow the execution of the resulting code, and programmers are largely limited to the feature set provided by the package library.[15]
  • Web browsers offer a convenient alternative to GUI packages for many applications. Web browsers utilize HTML as a presentation layer for applications hosted on a central server, and web browsers are available for most platforms.[16]
  • Tkinter is a Python binding to the Tk GUI toolkit. It is the standard Python interface to the Tk GUI toolkit, and is Python's de facto standard GUI.[17]
  • There are several popular GUI library alternatives available, such as wxPython, PyQt (PySide), Pygame, Pyglet, and PyGTK.[18]
  • A Tkinter top-level window is a window that exists independently on the screen. It will be decorated with the standard frame and controls for the desktop manager. It can be moved around the desktop, and can usually be resized.[19]
  • Widget is the generic term for any of the building blocks that make up an application in a graphical user interface. Examples of widgets: buttons, radiobuttons, text fields, frames, and text labels.[20]

Python GUI

edit
  • The tkinter.Tk() method creates a toplevel widget of Tk, which usually is the main window of an application.[21]
  • The root.title() method sets the window title.[22]
  • The maxsize() method returns the maximum size available for the window.[23]
  • The geometry() method sets the size and offset in pixels for the window.[24]
  • The tkinter.scrolledtext object implements a basic text widget with a vertical scroll bar.[25]
  • The pack() method packs widgets into the available window space.[26]
  • The scrolledtext.insert() method inserts text into the scrolled tex widget.

Key Terms

edit
button
Any graphical control element that provides the user a simple way to trigger an event.[27]
checkbutton
A GUI widget that permits the user to make a binary choice, i.e. a choice between one of two possible mutually exclusive options.[28]
entry
A graphical control element intended to enable the user to input text information to be used by the program.[29]
frame
A type of box within which a collection of graphical control elements can be grouped as a way to show relationships visually, either because the items are functionally related (such as a radio button), or because they apply to related objects.[30]
label
A graphical control element which displays text on a form. It is usually a static control; having no interactivity.[31]
radiobutton
A graphical control element that allows the user to choose only one of a predefined set of options.[32]
scrollbar
A widget in which continuous text, pictures, or other content can be scrolled in a predetermined direction (up, down, left, or right) on a computer display, window, or viewport so that all of the content can be viewed, even if only a fraction of the content can be seen on a device's screen at one time.[33]
window
A graphical control element which consists of a visual area containing some of the graphical user interface of the program it belongs to and is framed by window decoration.[34]

Review Questions

edit
Enable JavaScript to hide answers.
Click on a question to see the answer.
  1. A graphical user interface (GUI) is _____.
    A graphical user interface (GUI) is type of user interface that allows users to interact with electronic devices through graphical icons and visual indicators such as secondary notation, instead of text-based user interfaces, typed command labels or text navigation.
  2. Designing the visual composition and temporal behavior of a GUI is _____.
    Designing the visual composition and temporal behavior of a GUI is an important part of software application programming in the area of human–computer interaction.
  3. The goal of human-computer interaction is to _____.
    The goal of human-computer interaction is to enhance the efficiency and ease of use for the underlying logical design of a stored program, a design discipline named usability.
  4. Typically, users interact with information by _____.
    Typically, users interact with information by manipulating visual widgets that allow for interactions appropriate to the kind of data they hold.
  5. Large widgets, such as _____, usually _____. Smaller ones usually _____.
    Large widgets, such as windows, usually provide a frame or container for the main presentation content such as a web page, email message or drawing. Smaller ones usually act as a user-input tool.
  6. The most common combination of widget elements in GUIs is the _____ paradigm.
    The most common combination of widget elements in GUIs is the windows, icons, menus, pointer (WIMP) paradigm.
  7. A Platform Independent Graphical User Interface package is _____.
    A Platform Independent Graphical User Interface package is a software library that a programmer uses to produce GUI code for multiple computer platforms.
  8. Most platform-independent Graphical User Interface packages _____.
    Most platform-independent Graphical User Interface packages slow the execution of the resulting code, and programmers are largely limited to the feature set provided by the package library.
  9. Web browsers offer _____.
    Web browsers offer a convenient alternative to GUI packages for many applications. Web browsers utilize HTML as a presentation layer for applications hosted on a central server, and web browsers are available for most platforms.
  10. Tkinter is _____.
    Tkinter is a Python binding to the Tk GUI toolkit. It is the standard Python interface to the Tk GUI toolkit, and is Python's de facto standard GUI.
  11. There are several popular GUI library alternatives available, such as _____, _____, _____, _____, and _____.
    There are several popular GUI library alternatives available, such as wxPython, PyQt (PySide), Pygame, Pyglet, and PyGTK.
  12. A Tkinter top-level window is _____.
    A Tkinter top-level window is a window that exists independently on the screen. It will be decorated with the standard frame and controls for the desktop manager. It can be moved around the desktop, and can usually be resized.
  13. Widget is _____.
    Widget is the generic term for any of the building blocks that make up an application in a graphical user interface. Examples of widgets: buttons, radiobuttons, text fields, frames, and text labels.
  14. The tkinter.Tk() method _____.
    The tkinter.Tk() method creates a toplevel widget of Tk, which usually is the main window of an application.
  15. The root.title() method _____.
    The root.title() method sets the window title.
  16. The maxsize() method _____.
    The maxsize() method returns the maximum size available for the window.
  17. The geometry() method _____.
    The geometry() method sets the size and offset in pixels for the window.
  18. The tkinter.scrolledtext object _____.
    The tkinter.scrolledtext object implements a basic text widget with a vertical scroll bar.
  19. The pack() method _____.
    The pack() method packs widgets into the available window space.
  20. The scrolledtext.insert() method _____.
    The scrolledtext.insert() method inserts text into the scrolled tex widget.

Assessments

edit

See Also

edit

References

edit