Python GUI Tkinter Tutorial

Tkinter Introduction

Tkinter is an inbuilt Python module used to create simple GUI apps. It is the most commonly used module for GUI apps in the Python.

You don't need to worry about installation of the Tkinter module as it comes with Python default.

Tkinter is the Python port for Tcl-Tk GUI toolkit developed by Fredrik Lundh. This module is bundled with standard distributions of Python for all platforms.

An empty Tkinter top-level window can be created by using the following steps.

  • import the Tkinter module.
  • Create the main application window.
  • Add the widgets like labels, buttons, frames, etc. to the window.
  • Call the main event loop so that the actions can take place on the user's computer screen.

Tkinter provides the following widgets:

  • button
  • canvas
  • combo-box
  • frame
  • level
  • check-button
  • entry
  • level-frame
  • menu
  • list-box
  • menu button
  • message
  • tk_optionMenu
  • progress-bar
  • radio button
  • scroll bar
  • separator and
  • tree-view

Simple tk window Example:

You will see the title "Hello Python" because here we are using tkinter title which shows the title of the window.

Output:


Standard Attributed for GUI

  • Dimensions
  • Fonts
  • Colors
  • Cursors
  • Anchors
  • Bitmaps

Python Tkinter Geometry

  • The pack(): This method manages the geometry of widgets in blocks
  • The grid(): This method organizes widgets in a tabular structure
  • The place(): This method organizes the widgets to place them in a specific position

Tkinter also provides the bellow top-level windows:

  • tk_chooseColor - pops up a dialog box for the user to select a color.
  • tk_chooseDirectory - pops up a dialog box for the user to select a directory.
  • tk_dialog - creates a modal dialog and waits for a response.
  • tk_getOpenFile - pops up a dialog box for the user to select a file to open.
  • tk_getSaveFile - pops up a dialog box for the user to select a file to save.
  • tk_messageBox - pops up a message window and waits for a user response.
  • tk_popup - posts a popup menu.
  • toplevel - creates and manipulates toplevel widgets.