0% found this document useful (0 votes)
50 views31 pages

GUI Basics

Uploaded by

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

GUI Basics

Uploaded by

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

F-Assignment #1

Deadline: May 6, 2023


Graphical User Interface (GUI)
in Python
What is tkinter?
Tkinter is the Python interface for Tk. Tkinter is an acronym for
"Tk interface". Tk provides the following widgets:
• button • label • notebook • scrollbar
• canvas • labelframe • tk_optionMenu • separator
• checkbutton • listbox • panedwindow • sizegrip
• combobox • menu • progressbar • spinbox
• entry • menubutton • radiobutton • text
• frame • message • scale • treeview
tkinter Windows
Every tkinter application must have a window, and this window is also
called a root window. By using tkinter you can create that root window
by using Tk() class.

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

To initialize tkinter, we have to create


a Tk root widget, which is a window with
a title bar and other decoration provided
by the window manager. The root widget
has to be created before any other
widgets and there can only be one root
widget.

Our script will remain in the event loop


until we close the window.
tkinter Geometry()
This method is used to set the dimensions of the Tkinter window and is
used to set the position of the main window on the user’s desktop.

This method is used to


set the dimensions of
the 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

To initialize tkinter, we have to create


a Tk root widget, which is a window with
a title bar and other decoration provided
. by the window manager. The root widget
et s here
dg
o t her w i has to be created before any other
t
Inser widgets and there can only be one root
widget.

Our script will remain in the event loop


until we close the window.
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

The pack geometry manager acts based on the concept of using


up free space within the parent widget. When packing, you can
specify at which end of the free space to put the widget, and how
it will grow along with said free space (as the window itself grows
and shrinks). The geometry manager than assigns widgets into
said free space, leaving as little empty space as possible.
pack
• expand − When set to true, widget expands to fill any space not otherwise used in
widget's parent.
• fill − Determines whether widget fills any extra space allocated to it by the packer, or
keeps its own minimal dimensions:
• NONE (default)
• X (fill only horizontally)
• Y (fill only vertically)
• BOTH (fill both horizontally and vertically)
• side − Determines which side of the parent widget packs against:
• TOP (default)
• BOTTOM
• LEFT
• RIGHT
pack
grid
The grid—as the name suggests—treats the parent widget as a grid containing rows and
columns of cells. If you are familiar with spreadsheet software, grid will work in the same
way. 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).
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?)

Function or method to be called


when the button is clicked.
Buttons in Tkinter

Vertical padding Foreground color of


(10 px from the top) the button

Horizontal padding Background color of


(25 px from the left) the button
Input Textfields in Tkinter
End of Lecture
F-Assignment #5
PACK F-Assignment #6
GRID

Deadline:
June 1, 2024
SemiFinals - Project (individual)
Create a CALCULATOR

Deadline: May 13, 2023


SemiFinals
Reporting Groups: TOPIC #1
Topic Source - w3schools.com

Group 1 - May 2, 2023 (Inheritance)


Group 2 - May 2, 2023 (Iterators)
Group 3 - May 6, 2023 (Polymorphism)
Group 4 - May 6, 2023 (Scope)
Group 5 - May 9, 2023 (Modules)
Group 6 - May 9, 2023 (Dates)
Group 7 - May 13, 2023 (Math)
Group 8 - May 13, 2023 (JSON)
Finals
Reporting Groups: TOPIC #2
Topic Source - w3schools.com

Group 1 - May 16, 2023 (RegEx)


Group 2 - May 16, 2023 (PIP)
Group 3 - May 20, 2023 (Try...Except)
Group 4 - May 20, 2023 (String Formatting)
Group 5 - May 23, 2023 (File Handling)
Group 6 - May 23, 2023 (Read Files)
Group 7 - May 27, 2023 (Write/Create Files)
Group 8 - May 27, 2023 (Delete Files)
SemiFinals & Finals - New Groupings
(IT1 R10)
SemiFinals & Finals - New Groupings (IT121 - R11)

You might also like