0% found this document useful (0 votes)
2 views19 pages

Scientific Python 2025-03-04

The document provides an overview of various GUI libraries available for Python, including Tkinter, wxPython, PyQt, and others, highlighting their features and complexities. It discusses Tkinter's advantages such as accessibility, portability, and availability, as well as extensions like Pmw, ttk, and PIL that enhance its functionality. Additionally, it covers the basics of widget packing, event handling, and common widget classes in Tkinter.

Uploaded by

Maiky Khorani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views19 pages

Scientific Python 2025-03-04

The document provides an overview of various GUI libraries available for Python, including Tkinter, wxPython, PyQt, and others, highlighting their features and complexities. It discusses Tkinter's advantages such as accessibility, portability, and availability, as well as extensions like Pmw, ttk, and PIL that enhance its functionality. Additionally, it covers the basics of widget packing, event handling, and common widget classes in Tkinter.

Uploaded by

Maiky Khorani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Lession 6.

Tk-inter
4-3-2025

M. Maikey Zaki Bia


Graphical User Interface
Some portable GUI libraries available for Python
● The Tkinter module ('Tk interface')
○ Open source GUI library
○ Run portably on Windows, X Windows (Unix and Linux),
and Macintosh OS X
○ Basic tool, can be augmented with widget libraries
○ Mature, robust, widely used, and well documented
● wxPython
○ Python interface for the open source wxWidgets library,
which is a portable GUI class framework
○ The second most popular Python GUI toolkit today
○ Because wxPython is based on a C++ class library, most
observers consider it to be more complex than tkinter
Graphical User Interface
● PyQt
○ Python interface to the Qt toolkit
○ Runs portably today on Windows, OSX, and Unix/Linux
○ Like wxPython, Qt is generally more complex, yet more
feature rich, than tkinter
○ Qt grew up on Linux but became portable to other systems
○ Qt provides both GPL and LGPL open source licensing -
you must, for example, make your source code freely
available to end users
● PyGTK
○ Python interface to GTK, a portable GUI library originally
used as the core of the Gnome window system on Linux
○ PyGTK now runs on Windows and POSIX systems
Graphical User Interface
● Jython
○ Formerly known as JPython
○ Compiles Python source code to Java bytecode, and gives
Python scripts seamless access to Java class libraries on
the local machine.
○ swing and awt become available in Python
● IronPython
○ Python language for the .NET environment and runtime
engine, compiles Python programs to .NET bytecode
● PythonCard
○ An open source GUI builder and library built on top of the
wxPython toolkit
Tkinter pragmatics
Accessibility
● tkinter is generally regarded as a lightweight toolkit and one of the
simplest GUI solutions for Python available today. It is easy to get
started in tkinter right away, without first having to grasp a much
larger class interaction model.
Portability
● A Python script that builds a GUI with tkinter will run without
source code changes on all major windowing platforms today
Availability
● A standard Python module, shipped with the interpreter
● Because the underlying Tk library is also used by the Tcl and Perl
programming languages (and others), it tends to receive more
development resources than other toolkits available.
Tkinter extensions
Pmw
● Python Mega Widgets is an extension toolkit for building high-
level compound widgets
● It extends the tkinter API with a collection of more sophisticated
widgets for advanced GUI development and a framework for
implementing some of your own.
● To view its widgets and the corresponding code you use to
construct them, run the demos\All.py script in the Pmw distribution
package:

$ sudo pip3 install Pmw


Tkinter extensions
ttk
● ttk, is a relatively new widget set which attempts to separate the
code implementing a widget’s behavior from that implementing its
appearance
● Widget classes handle state and callback invocation, whereas
widget appearance is managed separately by themes.
● Comes with 17 widgets, 11 of which are already present in tkinter
and are meant as replacements for some of tkinter’s standard
widgets, and 6 of which are new—Combobox, Notebook,
Progressbar, Separator, Sizegrip and Treeview.
Tkinter extensions
PIL
● The Python Imaging Library (PIL) is an open source extension
package that adds image-processing tools to Python.
● Among other things, it provides tools for image thumbnails,
transforms, and conversions, and it extends the basic tkinter
image object to add support for displaying many image file types.
● Allows tkinter GUIs to display JPEG, TIFF, and PNG images not
supported by the base tkinter toolkit itself.
Tkinter: Resizing Basics
The function pack() accepts some paramerters like:

● expand=YES option
○ Asks the packer to expand the allocated space for the
widget in general into any unclaimed space in the widget’s
parent.
● fill option
○ Can be used to stretch the widget to occupy all of its
allocated space.

from tkinter import *


Label(text='Hello GUI world!').pack(expand=YES, fill=BOTH)
mainloop()
Tkinter: Calling pack()
We can call pack() on widgets without saving them:

● Label(text='hi').pack() # OK

● widget = Label(text='hi').pack() # wrong!

● Label(text='hi').pack().mainloop() # wrong!

● widget = Label(text='hi') # OK, use widget


widget.pack()
Tkinter: Packing Order
How does the packer’s layout system work?

1. The packer starts out with an available space cavity that includes
the entire parent container (e.g., the whole Frame or top-level
window).
2. As each widget is packed on a side, that widget is given the entire
requested side in the remaining space cavity, and the space
cavity is shrunk.
3. Later pack requests are given an entire side of what is left, after
earlier pack requests have shrunk the cavity.
4. After widgets are given cavity space, expand divides any space
left, and fill and anchor stretch and position widgets within their
assigned space.
Tkinter: Packing Order
Label(win, text='Hello container world!').pack(side=TOP)
Button(win, text='Hello', command=greeting).pack(side=LEFT)
Button(win, text='Quit', command=win.quit).pack(side=RIGHT)

Button(win, text='Hello', command=greeting).pack(side=LEFT)


Label(win, text='Hello container world!').pack(side=TOP)
Button(win, text='Quit', command=win.quit).pack(side=RIGHT)

Button(win, text='Hello', command=greeting).pack(side=LEFT)


Button(win, text='Quit', command=win.quit).pack(side=RIGHT)
Label(win, text='Hello container world!').pack(side=TOP)
Tkinter: Fill and expand
The fill option tells the manager that the widget wants fill the entire space
assigned to it. The value controls how to fill the space; BOTH means that the
widget should expand both horizontally and vertically, X means that it should
expand only horizontally, and Y means that it should expand only vertically.

The expand option tells the manager to assign additional space to the widget
box. If the parent widget is made larger than necessary to hold all packed
widgets, any exceeding space will be distributed among all widgets that have
the expand option set to a non-zero value.
Tkinter: Fill and expand
win = Frame()
win.pack()
Button(win, text='Hello', command=greeting).pack(side=LEFT,fill=Y)
Label(win, text='Hello container world').pack(side=TOP)
Button(win, text='Quit', command=win.quit).pack(side=RIGHT, expand=YES, fill=X)

win = Frame()
win.pack(side=TOP, expand=YES, fill=BOTH)
Button(win, text='Hello', command=greeting).pack(side=LEFT, fill=Y)
Label(win, text='Hello container world').pack(side=TOP, expand=YES,fill=BOTH)
Button(win, text='Quit', command=win.quit).pack(side=RIGHT, expand=YES, fill=X)
Tkinter:
Using Anchor to Position
● The anchor option accepts tkinter constants identifying all eight
points of the compass (N, NE, NW, S, etc.) and CENTER as its
value
● The Hello button is anchored to the north, side:left of its space
allocation. Because this button was packed first, it got the entire
left side of the parent frame. This is more space than is needed to
show the button, so it shows up in the middle of that side by
default
Tkinter: Redefine a Callback
● The HelloButton class inherits everything from the tkinter Button
class, but adds a callback method and constructor logic to set the
command option to self.callback, a bound method of the instance.
● Instead of exiting, this MyButton button, when pressed, prints to
stdout and stays up
Tkinter widget classes
Label A simple message area
Button A simple labeled push-button widget
Frame A container for attaching and arranging other widget objects
Toplevel, Tk A new window managed by the window manager
Message A multiline label
Entry A simple single-line text-entry field
Checkbutton A two-state button widget, typically used for multiple-choice selections
Radiobutton A two-state button widget, typically used for single-choice selections
Scale A slider widget with scalable positions
PhotoImage An image object used for displaying full-color images on other widgets
BitmapImage An image object used for displaying bitmap images on other widgets
Menu A set of options associated with a Menubutton or top-level window
Menubutton A button that opens a Menu of selectable options and submenus
Scrollbar A control for scrolling other widgets (e.g., listbox, canvas, text)
Listbox A list of selection names
Text A multiline text browse/edit widget, with support for fonts, and so on
Canvas A graphic drawing area, which supports lines, circles, photos, text, and so on
Tkinter: Events
Some frequently used events

● <ButtonRelease> fires when a button is released


● <ButtonPress> is run when the button first goes down
● <Motion> is triggered when a mouse pointer is moved.
● <Enter> and <Leave> handlers intercept mouse entry and exit in a window’s
display area
● <Configure> is invoked when the window is resized, repositioned, and so on
● <Destroy> is invoked when the window widget is destroyed
● <FocusIn> and <FocusOut> are run as the widget gains and loses focus.
● <Map> and <Unmap> are run when a window is opened and iconified.
● <Escape>, <BackSpace>, and <Tab> catch other special key presses.
● <Down>, <Left>, and <Right> catch other arrow key presses.
Tkinter: Events
Other event tringgering methods

● Modifiers can be added to event identifiers to make them even more


specific; for instance, <B1-Motion> means moving the mouse with the left
button pressed, and <KeyPress-a> refers to pressing the “a” key only.

● Synonyms can be used for some common event names; for instance,
<ButtonPress-1>, <Button-1>, and <1> mean a left mouse button press, and
<KeyPress-a> and <Key-a> mean the “a” key. All forms are case sensitive:
use <Key-Escape>, not <KEY-ESCAPE>.

● Virtual event identifiers can be defined within double bracket pairs (e.g.,
<<PasteText>>) to refer to a selection of one or more event sequences.

You might also like