0% found this document useful (0 votes)
22 views14 pages

Tkinter Introduction

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)
22 views14 pages

Tkinter Introduction

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/ 14

GUI in Python

Tk inter library
Learning objectives
• Understand the GUI in python
• Learn the basic feature of tkinter GUI to include the
following
• How to create a window
• Window background, size and title
• Labels
• Data entry boxes
• Buttons
• Message boxes
• Place objectives
• Add images
What is Tkinter?
• Before we begin, you ought to know
that the Tkinter module (“Tk
interface”) is the standard Python
interface to the Tk GUI toolkit
from scriptics (it was formerly
developed by Sun Labs).
• Simply put, Tkinter consists of a
number of modules.
• The public interface is provided
through a number of Python modules.
To use Tkinter, all you need to do is to
import the Tkinter module:
• import tkinter import *
Create a window in python using tkinter
Create a window background colour, Title and size
Create a Label

• A variable called label1


created to store the label. A
label is just text to be
displayed on screen.
• fg is the font colour, bg is the
background colour around
the font.
• It is an object, hence will not
show on screen without the
pack() code.
• Window should stay opened,
hence a mainloop() is
required.
Data Entry box

Data entry box used to enter data. It has been created


and packed. Remember programs always run in a
sequence, so pack should be after the object.
The .get() method will be used to collect data entered in
the textbox by the user later in the tutorial.
Two Data Entry boxes

Data entry boxes created and packed.


Remember programs always run in a
sequence.
Buttons

• Create a button and saves it in a


variable called button1. Command
part of the code requires a
subroutine called WhenPressed. It
should be created above the button
code.
• Remember to pack the button1 i.e.
button1.pack()
Subroutine for the Button
• Create a subroutine for the button called WhenPressed. Use the .get() method to
collect the user input and convert it into an integer with the .int() method. Collect
the user input and use them to find the square of a number.

Data entered in the data entry box should be


stored in a variable as in integer hence,
usernum.
Using Message Boxes

• When code is run, you


can enter 2 values in
the input boxes, then
click the button.
• When the button is
pressed, the .get()
method is used to get
the data, and a
message box is used to
display the strings and
calculate the square of
a number and output
the result.
Creating 3 widgets.
#import the 'tkinter module
import tkinter
#Create a window using the 'tkinter module
window = tkinter.Tk()
#Create the title for the window
window.title("Creating 3 Widgets - label, textentry and
button")
#Creating a label widget (label1)
label1=tkinter.Label(window, text="Label")
#Creating a text entry widget (txtentry)
txtentry=tkinter.Entry(window)
#Creating a button widget (called btn)
btn=tkinter.Button(window, text ="Button")

#Adding the widgets, in order to the created window.


Keyword =pack
label1.pack()
txtentry.pack()
btn.pack()
#Finally, draw the window + start the application
Types of Message Boxes
• To use a message box you must add a new import line
at the start of your program.
• The three main types of message box can be seen below:

The first string in the brackets is the title of the message box and, after the comma, the second
string is the message itself.

Use an if statement directly after a multiple choice text box to determine what happens:
Independent activity

Go to
https://www.csnewbs.com/python-1
1-gui
• Complete practise tasks 1 - 8

You might also like