0% found this document useful (0 votes)
23 views

10 Slide

This document discusses Tkinter, a Python GUI programming library. It covers getting started with Tkinter, processing events with the mainloop function, common widget classes like Button and Label, formatting text and images, drawing on canvases, and using grid and pack managers to layout widgets.

Uploaded by

kalaa0075
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)
23 views

10 Slide

This document discusses Tkinter, a Python GUI programming library. It covers getting started with Tkinter, processing events with the mainloop function, common widget classes like Button and Label, formatting text and images, drawing on canvases, and using grid and pack managers to layout widgets.

Uploaded by

kalaa0075
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/ 16

Chapter 10

Basic GUI Programming Using Tkinter

© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.


1
Motivations
Tkinter is not only a useful tool for developing
GUI projects, but also a valuable pedagogical
tool for learning object-oriented programming.

© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.


2
Getting Started with Tkinter
Getting started with Tkinter with a simple example.

© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.


3
Processing Events
window.mainloop() # Create an event loop
The statement creates an event loop. The event loop processes the
events continuously.
Start Event Loop

Event to false
Process?

Yes Process

No
Request to
Terminate?

Yes
ProcessButtonEvent

Terminate

© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.


4
The Widget Classes
Widget Class Description

Button A simple button, used to execute a command.

Checkbutton Clicking a check button toggles between the values.

Radiobutton Clicking a radio button sets the variable to that value,


and clears all other radio buttons associated with the
same variable.

Entry A text entry field, a.k.a a text field or a text box.

Frame A container widget for containing other widgets.

Menu A menu pane, used to implement pull down and popup menus.

Menubutton A menu button, used to implement pull down menus.

Label Displays a text or an image.

Message Displays a text. Similar to the label widget, but can


automatically wrap text to a given width or aspect ratio.

Text Formatted text display. Allows you to display and edit


text with various styles and attributes. Also supports
embedded images and windows.

Scale Allows you to set a numerical value by dragging a


"slider".

Canvas Structured graphics, used to draw graphs and plots,


create graphics editors, and to implement custom widgets.

Toplevel A container widget displayed as a separate, top-level


window.
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
5
Color and Font
To specify a color, you can either use a color name such as
red, yellow, green, blue, white, black, purple, etc, or
explicitly specify the red, green, and blue (RGB) color
components using a string #RRGGBB, where RR, GG, BB
are hexadecimal representations of the red, green and blue
values, respectively.

"Times 10 bold"
"Helvetica 10 bold italic"
"Courier New 20 bold italic"
"Courier New 20 bold italic over strike underline"
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
6
Text Formatting
The text in a label and a button is centered by default. You
can change it by using the justify option with values LEFT,
CENTER, or RIGHT. You can also display the text in
multiple lines by inserting the newline character \n to
separate texts.

© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.


7
Mouse Cursor
You can set a mouse cursor by using the cursor option with
values such as "arrow" (default), "circle", "cross" "plus",
etc.

© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.


8
Change Properties
widgetName["propertyName] = newPropertyValue

btShowOrHide = Button(window, text = "Show", bg =


"white")
btShowOrHide["text"] = "Hide"
btShowOrHide["bg"] = "red"
btShowOrHide["fg"] = "#AB84F9" # Change fg color to
#AB84F9
btShowOrHide["cursor"] = "plus" # Change mouse cursor to
plus

© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.


9
Canvas
Canvas can be used to display shapes. You can use the
method such as create_rectangle, create_oval,
create_arc, create_polygon, and create_line to draw a
rectangle, oval, arc, polygon, and line on a canvas.

(0, 0) Y Axis
X Axis

y
(x, y)
(0, 0) X Axis
Python Coordinate Conventional
System Coordinate
System
Y Axis

© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.


10
Canvas Demo

CanvasDemo

© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.


11
Drawing Methods
(x1 , y1 ) (x1, y1) (x1, y1)

(x2, y2) (x2, y2) (x2 , y2 )

canvas.create_rectangle(x1, y1, canvas.create_oval(x1, y1, x2, y2) canvas.create_arc(x1, y1, x2, y2, start, extent)
x2, y2)

(x2, y2)
(x1, y1) ABCDE
(x1, y1)
(x2, y2)
(x, y)
(x3, y3)

canvas.create_line(x1, y1, x2, y2) canvas.create_oval(x1, y1, x2, y2, x3, y3) canvas.create_text(x, y, text = "ABCDE")

© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.


12
Geometry Managers
Grid Manager
Pack Manager

Since each manager has its own style of placing


the widget, it is not a good practice to mix the
managers for the widgets in the same container.
You can use a frame as a sub-container to
achieve the desired layout.

© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.


13
Grid Managers

GridManagerDemo

© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.


14
Pack Managers

PackManagerDemo1

PackManagerDemo2

© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.


15
Display Images
You can add an image in a label, button, check
button, and radio button. To create an image, use
the PhotoImage class as follows:

photo = PhotoImage(file = imagefilename)

The image file must be GIF. You can use a


conversion utility to convert image files in other
format into GIF.
© Copyright 2022 by Pearson Education, Inc. All Rights Reserved.
16

You might also like