Python Tkinter

Posted : admin On 1/25/2022

Source code:Lib/tkinter/tix.py

  1. Python Tkinter Button
  2. Python Tkinter Treeview
  3. Python Tkinter Button
  4. Python Tkinter Vs Qt5
  5. Python Tkinter Ttk

Python - Tkinter Anchors - Anchors are used to define where text is positioned relative to a reference point. Tkinter (GUI Programming) Tkinter is a graphical user interface (GUI) module for Python, you can make desktop apps with Python. You can make windows, buttons, show text and images amongst other things. Tk and Tkinter apps can run on most Unix platforms. This also works on Windows and Mac OS X. The module Tkinter is an interface to the Tk GUI. 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. PyQtis, the Python interface to Qt, is a very popular cross-platform GUI framework. PyGTK is the module that ports Python to another popular GUI widget toolkit called GTK. Tk.call method is the Tkinter interface to tcl interpreter. We could run a tcl command by using this call method. It is handy when the Tkinter wrapper couldn’t have access to some tcl/tk features.

Deprecated since version 3.6: This Tk extension is unmaintained and should not be used in new code. Usetkinter.ttk instead.

The tkinter.tix (Tk Interface Extension) module provides an additionalrich set of widgets. Although the standard Tk library has many useful widgets,they are far from complete. The tkinter.tix library provides most of thecommonly needed widgets that are missing from standard Tk: HList,ComboBox, Control (a.k.a. SpinBox) and an assortment ofscrollable widgets.tkinter.tix also includes many more widgets that are generally useful ina wide range of applications: NoteBook, FileEntry,PanedWindow, etc; there are more than 40 of them.

With all these new widgets, you can introduce new interaction techniques intoapplications, creating more useful and more intuitive user interfaces. You candesign your application by choosing the most appropriate widgets to match thespecial needs of your application and users.

See also

Tix Homepage

The home page for Tix. This includes links to additional documentationand downloads.

Tix Man Pages

On-line version of the man pages and reference material.

Tix Programming Guide

On-line version of the programmer’s reference material.

Tix Development Applications
Python

Tix applications for development of Tix and Tkinter programs. Tide applicationswork under Tk or Tkinter, and include TixInspect, an inspector toremotely modify and debug Tix/Tk/Tkinter applications.

Using Tix¶

class tkinter.tix.Tk(screenName=None, baseName=None, className='Tix')

Toplevel widget of Tix which represents mostly the main window of anapplication. It has an associated Tcl interpreter.

Classes in the tkinter.tix module subclasses the classes in thetkinter. The former imports the latter, so to use tkinter.tixwith Tkinter, all you need to do is to import one module. In general, youcan just import tkinter.tix, and replace the toplevel call totkinter.Tk with tix.Tk:

To use tkinter.tix, you must have the Tix widgets installed, usuallyalongside your installation of the Tk widgets. To test your installation, trythe following:

Tix Widgets¶

Tixintroduces over 40 widget classes to the tkinter repertoire.

Basic Widgets¶

class tkinter.tix.Balloon

A Balloon thatpops up over a widget to provide help. When the user moves the cursor inside awidget to which a Balloon widget has been bound, a small pop-up window with adescriptive message will be shown on the screen.

class tkinter.tix.ButtonBox

The ButtonBoxwidget creates a box of buttons, such as is commonly used for OkCancel.

class tkinter.tix.ComboBox

The ComboBoxwidget is similar to the combo box control in MS Windows. The user can select achoice by either typing in the entry subwidget or selecting from the listboxsubwidget.

class tkinter.tix.Control

The Controlwidget is also known as the SpinBox widget. The user can adjust thevalue by pressing the two arrow buttons or by entering the value directly intothe entry. The new value will be checked against the user-defined upper andlower limits.

class tkinter.tix.LabelEntry

The LabelEntrywidget packages an entry widget and a label into one mega widget. It canbe used to simplify the creation of “entry-form” type of interface.

class tkinter.tix.LabelFrame

The LabelFramewidget packages a frame widget and a label into one mega widget. To createwidgets inside a LabelFrame widget, one creates the new widgets relative to theframe subwidget and manage them inside the frame subwidget.

class tkinter.tix.Meter

The Meter widgetcan be used to show the progress of a background job which may take a long timeto execute.

class tkinter.tix.OptionMenu

The OptionMenucreates a menu button of options.

class tkinter.tix.PopupMenu

The PopupMenuwidget can be used as a replacement of the tk_popup command. The advantageof the TixPopupMenu widget is it requires less application codeto manipulate.

class tkinter.tix.Select

The Select widgetis a container of button subwidgets. It can be used to provide radio-box orcheck-box style of selection options for the user.

class tkinter.tix.StdButtonBox

The StdButtonBoxwidget is a group of standard buttons for Motif-like dialog boxes.

File Selectors¶

class tkinter.tix.DirList

The DirListwidget displays a list view of a directory, its previous directories and itssub-directories. The user can choose one of the directories displayed in thelist or change to another directory.

class tkinter.tix.DirTree

The DirTreewidget displays a tree view of a directory, its previous directories and itssub-directories. The user can choose one of the directories displayed in thelist or change to another directory.

class tkinter.tix.DirSelectDialog

The DirSelectDialogwidget presents the directories in the file system in a dialog window. The usercan use this dialog window to navigate through the file system to select thedesired directory.

class tkinter.tix.DirSelectBox

The DirSelectBox is similar to the standard Motif(TM)directory-selection box. It is generally used for the user to choose adirectory. DirSelectBox stores the directories mostly recently selected intoa ComboBox widget so that they can be quickly selected again.

class tkinter.tix.ExFileSelectBox

The ExFileSelectBoxwidget is usually embedded in a tixExFileSelectDialog widget. It provides aconvenient method for the user to select files. The style of theExFileSelectBox widget is very similar to the standard file dialog onMS Windows 3.1.

class tkinter.tix.FileSelectBox

The FileSelectBoxis similar to the standard Motif(TM) file-selection box. It is generally usedfor the user to choose a file. FileSelectBox stores the files mostly recentlyselected into a ComboBox widget so that they can be quickly selectedagain.

Python Tkinter Button

class tkinter.tix.FileEntry

The FileEntrywidget can be used to input a filename. The user can type in the filenamemanually. Alternatively, the user can press the button widget that sits next tothe entry, which will bring up a file selection dialog.

Hierarchical ListBox¶

class tkinter.tix.HList

The HList widgetcan be used to display any data that have a hierarchical structure, for example,file system directory trees. The list entries are indented and connected bybranch lines according to their places in the hierarchy.

class tkinter.tix.CheckList

The CheckListwidget displays a list of items to be selected by the user. CheckList actssimilarly to the Tk checkbutton or radiobutton widgets, except it is capable ofhandling many more items than checkbuttons or radiobuttons.

class tkinter.tix.Tree

The Tree widgetcan be used to display hierarchical data in a tree form. The user can adjust theview of the tree by opening or closing parts of the tree.

Tabular ListBox¶

class tkinter.tix.TList

The TList widgetcan be used to display data in a tabular format. The list entries of aTList widget are similar to the entries in the Tk listbox widget. Themain differences are (1) the TList widget can display the list entriesin a two dimensional format and (2) you can use graphical images as well asmultiple colors and fonts for the list entries.

Manager Widgets¶

class tkinter.tix.PanedWindow

The PanedWindowwidget allows the user to interactively manipulate the sizes of several panes.The panes can be arranged either vertically or horizontally. The user changesthe sizes of the panes by dragging the resize handle between two panes.

class tkinter.tix.ListNoteBook

The ListNoteBookwidget is very similar to the TixNoteBook widget: it can be used todisplay many windows in a limited space using a notebook metaphor. The notebookis divided into a stack of pages (windows). At one time only one of these pagescan be shown. The user can navigate through these pages by choosing the name ofthe desired page in the hlist subwidget.

class tkinter.tix.NoteBook

The NoteBookwidget can be used to display many windows in a limited space using a notebookmetaphor. The notebook is divided into a stack of pages. At one time only one ofthese pages can be shown. The user can navigate through these pages by choosingthe visual “tabs” at the top of the NoteBook widget.

Image Types¶

The tkinter.tix module adds:

  • pixmapcapabilities to all tkinter.tix and tkinter widgets to createcolor images from XPM files.

  • Compound imagetypes can be used to create images that consists of multiple horizontal lines;each line is composed of a series of items (texts, bitmaps, images or spaces)arranged from left to right. For example, a compound image can be used todisplay a bitmap and a text string simultaneously in a Tk Buttonwidget.

Miscellaneous Widgets¶

class tkinter.tix.InputOnly

The InputOnlywidgets are to accept inputs from the user, which can be done with the bindcommand (Unix only).

Form Geometry Manager¶

In addition, tkinter.tix augments tkinter by providing:

class tkinter.tix.Form

The Form geometrymanager based on attachment rules for all Tk widgets.

Tix Commands¶

class tkinter.tix.tixCommand

The tix commands provideaccess to miscellaneous elements of Tix’s internal state and theTix application context. Most of the information manipulated by thesemethods pertains to the application as a whole, or to a screen or display,rather than to a particular window.

To view the current settings, the common usage is:

tixCommand.tix_configure(cnf=None, **kw)

Query or modify the configuration options of the Tix application context. If nooption is specified, returns a dictionary all of the available options. Ifoption is specified with no value, then the method returns a list describing theone named option (this list will be identical to the corresponding sublist ofthe value returned if no option is specified). If one or more option-valuepairs are specified, then the method modifies the given option(s) to have thegiven value(s); in this case the method returns an empty string. Option may beany of the configuration options.

tixCommand.tix_cget(option)

Returns the current value of the configuration option given by option. Optionmay be any of the configuration options.

tixCommand.tix_getbitmap(name)

Locates a bitmap file of the name name.xpm or name in one of the bitmapdirectories (see the tix_addbitmapdir() method). By usingtix_getbitmap(), you can avoid hard coding the pathnames of the bitmapfiles in your application. When successful, it returns the complete pathname ofthe bitmap file, prefixed with the character @. The returned value can beused to configure the bitmap option of the Tk and Tix widgets.

tixCommand.tix_addbitmapdir(directory)

Tix maintains a list of directories under which the tix_getimage() andtix_getbitmap() methods will search for image files. The standard bitmapdirectory is $TIX_LIBRARY/bitmaps. The tix_addbitmapdir() methodadds directory into this list. By using this method, the image files of anapplications can also be located using the tix_getimage() ortix_getbitmap() method.

tixCommand.tix_filedialog([dlgclass])

Returns the file selection dialog that may be shared among different calls fromthis application. This method will create a file selection dialog widget whenit is called the first time. This dialog will be returned by all subsequentcalls to tix_filedialog(). An optional dlgclass parameter can be passedas a string to specified what type of file selection dialog widget is desired.Possible options are tix, FileSelectDialog or tixExFileSelectDialog.

tixCommand.tix_getimage(self, name)

Locates an image file of the name name.xpm, name.xbm orname.ppm in one of the bitmap directories (see thetix_addbitmapdir() method above). If more than one file with the same name(but different extensions) exist, then the image type is chosen according to thedepth of the X display: xbm images are chosen on monochrome displays and colorimages are chosen on color displays. By using tix_getimage(), you canavoid hard coding the pathnames of the image files in your application. Whensuccessful, this method returns the name of the newly created image, which canbe used to configure the image option of the Tk and Tix widgets.

tixCommand.tix_option_get(name)

Gets the options maintained by the Tix scheme mechanism.

tixCommand.tix_resetoptions(newScheme, newFontSet[, newScmPrio])

Resets the scheme and fontset of the Tix application to newScheme andnewFontSet, respectively. This affects only those widgets created after thiscall. Therefore, it is best to call the resetoptions method before the creationof any widgets in a Tix application.

The optional parameter newScmPrio can be given to reset the priority level ofthe Tk options set by the Tix schemes.

Because of the way Tk handles the X option database, after Tix has been hasimported and inited, it is not possible to reset the color schemes and font setsusing the tix_config() method. Instead, the tix_resetoptions()method must be used.

Tkinter tutorial provides basic and advanced concepts of Python Tkinter. Our Tkinter tutorial is designed for beginners and professionals.

Python provides the standard library Tkinter for creating the graphical user interface for desktop based applications.

Developing desktop based applications with python Tkinter is not a complex task. An empty Tkinter top-level window can be created by using the following steps.

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

Example

Output:

Tkinter widgets

Python Tkinter Treeview

There are various widgets like button, canvas, checkbutton, entry, etc. that are used to build the python GUI applications.

SNWidgetDescription
1ButtonThe Button is used to add various kinds of buttons to the python application.
2CanvasThe canvas widget is used to draw the canvas on the window.
3CheckbuttonThe Checkbutton is used to display the CheckButton on the window.
4EntryThe entry widget is used to display the single-line text field to the user. It is commonly used to accept user values.
5FrameIt can be defined as a container to which, another widget can be added and organized.
6LabelA label is a text used to display some message or information about the other widgets.
7ListBoxThe ListBox widget is used to display a list of options to the user.
8MenubuttonThe Menubutton is used to display the menu items to the user.
9MenuIt is used to add menu items to the user.
10MessageThe Message widget is used to display the message-box to the user.
11RadiobuttonThe Radiobutton is different from a checkbutton. Here, the user is provided with various options and the user can select only one option among them.
12ScaleIt is used to provide the slider to the user.
13ScrollbarIt provides the scrollbar to the user so that the user can scroll the window up and down.
14TextIt is different from Entry because it provides a multi-line text field to the user so that the user can write the text and edit the text inside it.
14ToplevelIt is used to create a separate window container.
15SpinboxIt is an entry widget used to select from options of values.
16PanedWindowIt is like a container widget that contains horizontal or vertical panes.
17LabelFrameA LabelFrame is a container widget that acts as the container
18MessageBoxThis module is used to display the message-box in the desktop based applications.

Python Tkinter Geometry

The Tkinter geometry specifies the method by using which, the widgets are represented on display. The python Tkinter provides the following geometry methods.

  1. The pack() method
  2. The grid() method
  3. The place() method

Let's discuss each one of them in detail.

Python Tkinter pack() method

The pack() widget is used to organize widget in the block. The positions widgets added to the python application using the pack() method can be controlled by using the various options specified in the method call.

However, the controls are less and widgets are generally added in the less organized manner.

The syntax to use the pack() is given below.

syntax

A list of possible options that can be passed in pack() is given below.

  • expand: If the expand is set to true, the widget expands to fill any space.
  • Fill: By default, the fill is set to NONE. However, we can set it to X or Y to determine whether the widget contains any extra space.
  • size: it represents the side of the parent to which the widget is to be placed on the window.

Example

Output:

Python Tkinter grid() method

The grid() geometry manager organizes the widgets in the tabular form. We can specify the rows and columns as the options in the method call. We can also specify the column span (width) or rowspan(height) of a widget.

This is a more organized way to place the widgets to the python application. The syntax to use the grid() is given below.

Syntax

Python Tkinter Button

A list of possible options that can be passed inside the grid() method is given below.

  • Column
    The column number in which the widget is to be placed. The leftmost column is represented by 0.
  • Columnspan
    The width of the widget. It represents the number of columns up to which, the column is expanded.
  • ipadx, ipady
    It represents the number of pixels to pad the widget inside the widget's border.
  • padx, pady
    It represents the number of pixels to pad the widget outside the widget's border.
  • row
    The row number in which the widget is to be placed. The topmost row is represented by 0.
  • rowspan
    The height of the widget, i.e. the number of the row up to which the widget is expanded.
  • Sticky
    If the cell is larger than a widget, then sticky is used to specify the position of the widget inside the cell. It may be the concatenation of the sticky letters representing the position of the widget. It may be N, E, W, S, NE, NW, NS, EW, ES.

Example

Output:

Python Tkinter place() method

The place() geometry manager organizes the widgets to the specific x and y coordinates.

Syntax

A list of possible options is given below.

Python Tkinter Vs Qt5

  • Anchor: It represents the exact position of the widget within the container. The default value (direction) is NW (the upper left corner)
  • bordermode: The default value of the border type is INSIDE that refers to ignore the parent's inside the border. The other option is OUTSIDE.
  • height, width: It refers to the height and width in pixels.
  • relheight, relwidth: It is represented as the float between 0.0 and 1.0 indicating the fraction of the parent's height and width.
  • relx, rely: It is represented as the float between 0.0 and 1.0 that is the offset in the horizontal and vertical direction.
  • x, y: It refers to the horizontal and vertical offset in the pixels.

Example

Python Tkinter Ttk

Output:

Prerequisite

Before learning Tkinter, you must have the basic knowledge of Python.

Audience

Our Python Tkinter tutorial is designed to help beginners and professionals.

Problem

We assure that you will not find any problem in this Tkinter tutorial. But if there is any mistake, please post the problem in contact form.