Python Topic 4
Python Topic 4
SCHOOL OF COMPUTING
GUI PROGRAMMING
GUI Programming in Python - Introduction to GUI library - Layout management - Events and
bindings - Fonts – Colours - Canvas - Widgets (frame, label, button, check box, entry, list box,
message, radio button, text, spin box)
S11BLH21- Programming in python
In python text only programs can be created using Command line Interface.
Graphical user interface(GUI) can be created using tkinter module in python.
• button
• canvas
• checkbutton
• combobox
• entry
• frame
• label
• listbox
• menu
• message
• progressbar
• radiobutton
S11BLH21- Programming in python
• scrollbar
• spinbox
• text
Layout Management
1. Pack Layout
2. Grid Layout
3. Place Layout
Example:
Output:
Fig 3.1
Explanation: Label l1 has been placed in top position, it is filled in X axis. Label l2
has been placed in Right Position and it is also filled in X axis. Since expand attribute
has the value True for Label l1,it can be stretched.
S11BLH21- Programming in python
1. Internal Padding
2. External padding
3. Padding in X Direction.
4. Padding in Y Direction.
Example:
Output:
S11BLH21- Programming in python
Fig 3.2
Example:
top=Tk()
l1=Label(top,text="Label1",bg="blue")
l2=Label(top,text="Label2",bg="red" )
l1.pack(fill=X,side=TOP,expand=True,pady=10)
l2.pack(fill=X,side=TOP,pady=10)
top.mainloop()
Output:
Fig 3.3
Example:
top=Tk()
l1=Label(top,text="Label1",bg="blue")
l2=Label(top,text="Label2",bg="red" )
l1.pack(fill=X,side=TOP,expand=True,ipadx=10)
l2.pack(fill=X,side=TOP,ipadx=10)
top.mainloop()
Output:
Fig 3.4
Example:
top=Tk()
l1=Label(top,text="Label1",bg="blue")
l2=Label(top,text="Label2",bg="red" )
l1.pack(fill=X,side=TOP,expand=True,ipadx=10)
l2.pack(fill=X,side=TOP,ipady=10)
top.mainloop()
Output:
The output is shown in Fig 3.5.
S11BLH21- Programming in python
Fig 3.5
Place Layout:
Place is the most complex manager out of the 3 managers. It uses absolute
positioning, when we choose place lay out in our design, then we need to specify the
position of the widgets using x and y coordinates. The size and position of the widgets
will not be changed when we resize the window.
Example:
from tkinter import *
top=Tk()
l1=Label(top,text="Label1",bg="blue")
l2=Label(top,text="Label2",bg="red" )
l1.place(x=10,y=50)
l2.place(x=10,y=100)
top.mainloop()
Output:
The output is shown in Fig 3.6.
S11BLH21- Programming in python
Fig 3.6
Explanation:
Here Label1 is placed in the position (10,50) and label2 is placed in the
position (10,100).
Grid Layout
Pack Layout is not easy to understand and it is difficult to change the existing
design. By using place layout, we can control the positioning of widgets but it is
complex than pack. Grid is one of the most versatile layout manager out of the three
layout managers in python. By using Grid layout, the widgets can be placed in rows
and columns.
S11BLH21- Programming in python
Example:
Fig 3.7
S11BLH21- Programming in python
Explanation:
Here Label 1 is placed in 0th row and 0th column. Label 2 is placed in 0th row and 1st
column and Label 3 is placed in 1st row and 1st column.
FONT
Ex: t =(“Arial”,14,”Bold”)
Example:
Output:
Fig 3.8
Explanation:
Text for the Button has been set in the Arial font with size 16 and Bold style.
Font Object
Import tkFont
Font f1=tkFont.Font(parameters,…..)
size − The font height as an integer in points. To get a font n pixels high,
use -n.
Example:
X Window Fonts:
If you are running under the X Window System, you can use any of the X font
names.
S11BLH21- Programming in python
COLORS
Tkinter represents colors with strings. There are two general ways to specify colors
in Tkinter :
• We can use a string specifying the proportion of red, green and blue in
hexadecimal digits. For example,
• "#fff" -- white,
• "#000000" -- black,
• "#000fff000" -- pure green
• "#00ffff" -- pure cyan
• We can also use any locally defined standard following color names.
▪ "white"
▪ "black"
▪ "red"
▪ "green"
▪ "blue"
▪ "cyan"
▪ "yellow"
▪ "magenta"
S11BLH21- Programming in python
Active background − Specifies Background color for the widget when the
widget is active.
background − Specifies Background color for the widget. This can also
be represented as bg.
foreground − Specifies Foreground color for the widget. This can also
be represented as fg.
Example:
Fig 3.9
Explanation:
Here the back ground of the button is red in color and foreground color of the button
is white in colour.
S11BLH21- Programming in python
CANVAS
The Canvas is a rectangular area used for drawing pictures or other complex
layouts. Graphics, text, widgets or frames can be placed on a Canvas.
Syntax:
w = Canvas ( top, option=value, ... )
top – It represents the parent window.
Options − commonly used options for this widget. These options can be used
as key-value pairs separated by commas.
1.Arc
2.Image
3.Line
4.Oval
5.Polygon
S11BLH21- Programming in python
ARC
Creates an arc item, which can be a chord or a simple arc.
Syntax:
Example:
Output:
Fig 3.10
Explanation:
Here Arc is drawn with blue color and within the bounded rectangle with top
left(10,50)position and bottom right(240,210) position and started from angle 0 and
extended till 150 degree.
Image
Syntax:
Create_image(x,y,options….)
image=image object
Example:
root=Tk()
w.create_image("F:\img2",50,50)
w.pack()
root.mainloop()
Line
Syntax:
x0,y0,x1,y1->coordinates of line
Example:
Fig 3.11
S11BLH21- Programming in python
OVAL
Creates a circle or an ellipse at the given coordinates. It takes two pairs of coordinates;
the top left and bottom right corners of the bounding rectangle for the oval.
Syntax:
x0, y0, x1, y1- the top left and bottom right corners of the bounding rectangle
Options:
Example:
Fig 3.12
Polygon
Syntax:
Options:
Example
WIDGETS IN PYTHON
Widgets are standard graphical user interface (GUI) elements, like different kinds of
buttons and menus.
Label
A Label widget shows text to the user about other widgets used in the application.
The widget can be updated programmatically.
Table 3.1
Option Description
anchor It specifies the exact position of the text within the size provided to the
widget. The default value is CENTER, which is used to center the text
within the specified space.
font Specifies font type of the text written inside the widget
Example:
Fig 3.13
Explanation:
Here Label has been created with green background color and white foreground
color with the text “Enter User Name”.
S11BLH21- Programming in python
ENTRY
The Entry widget is used to create the single line text-box to the user to accept a value
from the user. It can accept the text strings from the user. It can receive one line of text
from the user. For multiple lines of text, the text widget will be used.
w=Entry(root, options)
root-Main Window
Table 3.2
Optio Description
n
bg Specifies background color of the widget
font Specifies font type of the text written inside the widget
Option Description
relief Specifies type of border
text Text to be displayed in the widget
undeline Underline the label text
selectbackground Background color of the selected text
selectforeground Foreground color of the selected text
show Specifies the character used to mask characters in the text box
Example:
Fig 3.14
Explanation:
Here Label and entry widgets are created.Since the show attribute value is *,the
characters entered in the text box appeared as “*”.
Button
Button Widget is used to create various kinds of buttons.The user can interact with
the button.They can contain text or images.
b=Button(root,options)
root-main window
Table 3.3
Option Description
bg Specifies background color of the widget
bd Specifies border width. Default is 2 pixels
cursor Specifies type of cursor.eg:dot,arrow,circle
font Specifies font type of the text written inside the widget
fg Foreground color of the widget
height Height of the widget
width Width of the widget
image Specifies image to be displayed in the widget
padx Horizontal padding of text
pady Vertical padding of text
relief Specifies type of border
text Text to be displayed in the widget
underline Underline the label text
command It is set to function name which will be called the button is
clicked
Example:
S11BLH21- Programming in python
Output:
The output is shown in Fig 3.15.
S11BLH21- Programming in python
Checkbutton
The Checkbutton is used to track the user's choices provided to the application.
Checkbutton is used to implement the on/off selections. TheCheckbutton can contain
the or images or text. The Checkbutton is mostly used to provide many choices to the
user among which, the user needs to choose the one.
b=CheckButton(root,options)
root-main window
S11BLH21- Programming in python
Option Description
bg Specifies background color of the widget
bd Specifies border width. Default is 2 pixels
cursor Specifies type of cursor.eg:dot,arrow,circle
font Specifies font type of the text written inside the widget
fg Foreground color of the widget
height Height of the widget
width Width of the widget
image Specifies image to be displayed in the widget
padx Horizontal padding of text
pady Vertical padding of text
relief Specifies type of border
text Text to be displayed in the widget
undeline Underline the label text
command It is set to function name whicjh will be called the button is clicked
offvalue Set value to the control variable if the button is checked.Default
Value is 1
onvalue Set value to the control variable if the button is unchecked.Default
Value is 0
selectcolor Set color of the check button when it is checked.
selectimage Set the image to be shown when it is checked.
S11BLH21- Programming in python
Example:
Fig 3.16
S11BLH21- Programming in python
Radiobutton
b=RadioButton(root,options)
root-main window
Table 3.3
Option Description
bg Specifies background color of the widget
bd Specifies border width. Default is 2 pixels
cursor Specifies type of cursor.eg:dot,arrow,circle
font Specifies font type of the text written inside the widget
fg Foreground color of the widget
height Height of the widget
width Width of the widget
image Specifies image to be displayed in the widget
S11BLH21- Programming in python
Option Description
padx Horizontal padding of text
pady Vertical padding of text
relief Specifies type of border
text Text to be displayed in the widget
underline Underline the label text
command It is set to function name whicjh will be called the button is
clicked
value Set value to the control variable if the button is selected.
selectcolor Set color of the check button when it is checked.
selectimage Set the image to be shown when it is checked.
variable It is used to keep track of user choices.
Example:
from tkinter import *
root=Tk()
r1 = Radiobutton(root, text = "C", value = 1, height = 2, width = 10)
r2 = Radiobutton(root, text = "C++", value = 2, height = 2, width = 10)
r3 = Radiobutton(root, text = "JAVA",value = 3, height = 2, width = 10)
r1.pack()
r2.pack()
r3.pack()
root.mainloop()
S11BLH21- Programming in python
Output:
Fig 3.17
Listbox
The Listbox widget is used to display the list items to the user. The user can choose
one or more items from the list depending upon the configuration.
b=Listbox(root,options)
root-main window
S11BLH21- Programming in python
Table 3.4
Option Description
font Specifies font type of the text written inside the widget
Example:
Output:
Message
Its functionality is very similar to Label widget, except that it can automatically wrap
the text, maintaining a given width.
m=Message(root,options)
root-main window
Table 3.5
Option Description
bg Specifies background color of the widget
bd Specifies border width. Default is 2 pixels
cursor Specifies type of cursor.eg:dot,arrow,circle
font Specifies font type of the text written inside the widget
fg Foreground color of the widget
height Height of the widget
width Width of the widget
image Specifies image to be displayed in the widget
padx Horizontal padding of text
S11BLH21- Programming in python
Option Description
Output:
Fig 3.19
S11BLH21- Programming in python
Text
Tkinter provides us the Entry widget which is used to implement the single line text
box. Text widget provides advanced capabilities that allow us to edit a multiline text
and format the way it has to be displayed, such as changing its color and font. We can
also use the structures like tabs and marks to locate specific sections of the text, and
apply changes to those areas.
T=Text(root,options)
root-main window
Table 3.6
Option Description
bg Specifies background color of the widget
bd Specifies border width. Default is 2 pixels
cursor Specifies type of cursor.eg:dot,arrow,circle
font Specifies font type of the text written inside the widget
fg Foreground color of the widget
height Height of the widget
width Width of the widget
image Specifies image to be displayed in the widget
padx Horizontal padding of text
S11BLH21- Programming in python
Option Description
pady Vertical padding of text
relief Specifies type of border
xscrollcommand User can scroll the text widget horizontally
yscrollcommand User can scroll the text widget vertically
selectbackground Background color of the selected text
Table 3.7
Method Description
insert(index, string) It is used to insert the specified string at the given index.
Marks are used to bookmark the specified position between the characters of the
associated text.List of Mark handling methods are given in Table 3.8.
S11BLH21- Programming in python
Table 3.8
Method Description
The tags are the names given to the specific areas of the text. The tags are used to
configure the different areas of the text separately. The list of tag-handling methods
are given in Table 3.9.
Table 3.9
Method Description
Example:
Fig 3.20
S11BLH21- Programming in python
Explanation:
The tag “Write Here” tags the characters from the index 0 to 4.The tag “Click Here”
tags the characters from the index 8 to 13.These tags are configured using the method
tag_config().
3.6.1 SPINBOX
The Spinbox control is an alternative to the Entry control. It provides the range of
values to the user, out of which, the user can select only one value.It is used in the
case where a user is given some fixed number of values to choose from.
S=Spinbox(root,options)
root-main window
Table 3.10
Option Description
bg Specifies background color of the widget
bd Specifies border width. Default is 2 pixels
Option Description
height Height of the widget
width Width of the widget
values It represents the tuple containing the values for this widget.
Example:
Output:
Fig 3.21
3.6.2 Frame
Frame widget is used to organize the group of widgets. It acts like a container which
can be used to hold the other widgets. The rectangular areas of the screen are used to
organize the widgets to the python application.
S=Frame(root,options)
root-main window
Table 3.11
Option Description
bg Specifies background color of the widget
bd Specifies border width. Default is 2 pixels
cursor Specifies type of cursor.eg:dot,arrow,circle
height Height of the widget
width Width of the widget
Relief Specifies type of border
Example:
from tkinter import *
top = Tk()
Topframe = Frame(top)
Topframe.pack(side = TOP)
Bottomframe = Frame(top)
Bottomframe.pack(side =BOTTOM)
btn1 = Button(Topframe, text="Submit", fg="red",activebackground = "red")
btn1.pack(side = LEFT)
btn2 = Button(Topframe, text="Remove", fg="brown", activebackground = "brown")
btn2.pack(side = RIGHT)
btn3 = Button(Bottomframe, text="Add", fg="blue", activebackground = "blue")
btn3.pack(side = LEFT)
S11BLH21- Programming in python
Fig 3.23
Explanation:
Here two frames (Top Frame and Bottom Frame) have been created.Topframe contains
submit and remove buttons and Bottom frame contains Add and modify buttons .
S11BLH21- Programming in python
widget.bind(event, handler)
If an event matching the event description occurs in the widget, the given handler is
called with an object describing the event.
Example:
from tkinter import *
from tkinter.ttk import *
# creates tkinter window or root window
root = Tk()
# function to be called when button-2 of mouse is pressed
def pressed2(event):
print('Button-2 pressed at x = % d, y = % d'%(event.x, event.y))
# function to be called when button-3 of mouse is pressed
def pressed3(event):
print('Button-3 pressed at x = % d, y = % d'%(event.x, event.y))
## function to be called when button-1 is double clocked
defdouble_click(event):
S11BLH21- Programming in python
Output:
Fig 3.24
S11BLH21- Programming in python
Example:
mainloop()
Output:
Fig 3.25