GUI Programming with Python's Tkinter Library, Lecture notes of Programming Languages

An introduction to gui programming in python using the tkinter library. It covers the basics of creating a main window, adding widgets such as labels, buttons, and text entry fields, and using different geometry managers (pack, grid, and place) to position the widgets. The document also demonstrates how to create tkinter-compatible variables, display messages, get user input, and implement widgets like scales, checkboxes, and radio buttons. Several example applications are provided, including a feet-to-meters converter, a palindrome checker, and a term frequencies plotter. This comprehensive guide would be useful for students and developers looking to learn gui programming with python.

Typology: Lecture notes

2023/2024

Uploaded on 08/26/2024

adam-mustafa-2
adam-mustafa-2 🇵🇸

3 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
SELECTED
PROGRAMMING
LANGUAGES – PYTHON
Lecture 06
Dr. Mohammed Maree
Associate Professor in Information Technology
Faculty of Engineering & Information Technology
Arab American University, Palestine
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download GUI Programming with Python's Tkinter Library and more Lecture notes Programming Languages in PDF only on Docsity!

SELECTED

PROGRAMMING

LANGUAGES – PYTHON

Lecture 06

Dr. Mohammed Maree Associate Professor in Information Technology Faculty of Engineering & Information Technology Arab American University, Palestine

GUI Programming

 (^) Tkinter (Tk) is Python’s default GUI library.  (^) It is based on the Tk toolkit, originally designed for the Tool Command Language (Tcl).  (^) Once you have designed the application and the look and feel that goes along with your program, you will use basic building blocks known as widgets (classes) to piece together the desired components, and finally, to attach functionality to “make it real”.  (^) A widget is typically any part of the application that needs to be drawn onto the screen, including the main window.  (^) The syntax that is used to add a widget is as follows: my_widget = tk.Widget-name( its container window, ** its configuration options)

GUI Programming

 (^) The first line imported the tkinter module into the namespace with tk as its alias.  (^) The second line created an instance of the tkinter.Tk class. This created what is called the root window, which is shown in the screenshot.  (^) The third line executed the mainloop (that is, the event loop) method of the root object. The mainloop method is what keeps the root window visible.

GUI Programming

 (^) Once you have a main window defined, you can begin adding other widgets into it.  (^) All other widgets must belong to a parent which has the ability to display them, such as a Tk or Frame.  (^) Each widget is only visible if its parent is. This allows us to group widgets into different screens and show or hide groups of them as need be.  (^) Widgets are placed into their parents using special functions called geometry managers.  (^) There are three geometry managers available in Tkinter – pack, grid, and place.  (^) Geometry managers serve the purpose of deciding where in the parent widget to render its children.  (^) Each of the three geometry managers (pack, grid, and place)

GUI Programming - Geometry Managers - grid

 (^) The grid — as the name suggests — treats the parent widget as a grid containing rows and columns of cells.  (^) The grid lines will not be visible, they are just conceptual. To specify the position within the grid, the row and column keywords are used. These accept integer values and begin at 0 , not 1.  (^) A widget placed with grid( row=0, column=0) will be to the left of a widget at grid( row=0, column=1). Underneath these would sit a widget placed at grid( row=1, column=0).  (^) To make a widget span more than one cell, use columnspan for a horizontal size increase and rowspan for a vertical increase. So, to make our hypothetical bottom widget sit below both, the full argument set would be grid( row=1, column=0, columnspan=2).

8

GUI Programming - Geometry Managers - place

 (^) Unlike pack and grid, which automatically calculate where each new widget is added, place can be used in order to specify an exact location for a particular widget.  (^) place takes either x and y coordinates (in pixels) to specify an exact spot, which will not change as the window is resized, or relative arguments to its parent, allowing the widget to move with the size of the window.  (^) To place a widget at (5, 10) within the window, you would write widget.place( x=5, y=10).  (^) To keep a widget in the direct center, you would use widget.place( relx=0.5, rely=0.5).  (^) place also takes sizing options, so to keep a widget at 50 percent width and 25 percent height of the window, add (relwidth=0.5, relheight=0.25).  (^) place is rarely used in bigger applications due to its lack of

GUI Programming – A “Hello World” App

 (^) fill: This tells the widget to take all the space in both directions.  (^) expand: This tells the widget to expand when the window is resized.  (^) padx: Padding (empty space) of 100 pixels in the x direction (left, right). pady: Padding of 50 pixels in the y direction (above, below).

GUI Programming – A “Hello World” App – Adding

Interactivity

Creating Tkinter-compatible variables

Showing Messages

Getting Input Text

 (^) An Entry widget is a one-line text entry box which is put into a parent widget just like a Label or Button. The special Tkinter variables can be attached to an Entry to make getting the value out a breeze.  (^) Next 

Getting Input Text

Checkbotton and Radiobutton Widgets

Checkbotton and Radiobutton Widgets