GUI Basics
GUI Basics
tkinter window act as a container, where you will put all of your
widgets (Frames, Button, Label, Listbox, RadioButton, etc.)
Let’s get started with Tkinter
imports all tkinter module/widgets tkinter
window
height = 400px
width = 300px
Geometry Managers
Tk also provides three geometry managers:
• pack - which packs widgets into a cavity
• grid - which arranges widgets in a grid
• place - which positions widgets at absolute locations
Let’s get started with Tkinter
imports all tkinter module/widgets
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).
grid
grid
Remember…
The pack and grid are both intended to lay out the entire content of a parent
widget and apply different logic to decide where each new widget added should go.
For this reason, they cannot be combined inside the same parent. Once one widget
is inserted using pack or grid, all other widgets must use the same geometry
manager.
To pack or to grid?
• Using pack versus grid in your application is mostly down to personal
preference. There doesn't seem to be a particularly dominant reason to use one
over the other.
• The main advantage of pack is the code tends to be very readable. pack uses
words such as left and top to make it clear where the programmer wants the
widget to go.
• The great advantage of grid is its code simplicity to layout complexity ratio.
Without the need to split your application into frames, you can save many lines of
code and lay out a complicated window design with essentially just one line of
code per widget. You also don't need to worry about the order in which you add
your widgets to their parent as the numerical grid system will apply regardless.
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.
place is rarely used in bigger applications due to its lack of flexibility. It can be
tiresome keeping track of exact coordinates for a widget, and as things change with
the application, widgets may resize, causing unintended overlapping. For a smaller
window with only one or two widgets – say a custom pop-up message – place
could be a viable choice of geometry manager, since it allows for very easy
centering of said widgets.
place
x
y
Pack, Grid, or Place?
Source: http://gatherworkshops.github.io/programming/courses/tkinter/assets/images/geometry-managers.svg
Labels in Tkinter
A Label is a Tkinter Widget class, which is used to display text or an image. The
label is a widget that the user just views but not interact with.
Creating the label.
Sample Code: • The first parameter of the Label call Output:
is the name of the parent window, in
our case "root". So our Label widget
is a child of the root widget.
• The keyword parameter "text"
specifies the text to be shown
Buttons in Tkinter
The Button widget is used to add buttons in a Python application. These buttons
can display text or images that convey the purpose of the buttons.
Sample Code: Output:
Creating a Button
Horizontal padding
Vertical padding
Buttons in Tkinter
The Button widget is used to add buttons in a Python application. These buttons
can display text or images that convey the purpose of the buttons.
Sample Code: Output:
Function (What do you
want your button to do?)
Deadline:
June 1, 2024
SemiFinals - Project (individual)
Create a CALCULATOR