Building GUI Using Python
Building GUI Using Python
iv
er
si
ty
st
ud
y.
in
GUI Using Python
• Tkinter: Tkinter is the Python interface to the
in
Tk GUI toolkit shipped with Python. We would
y.
ud
look this option in this chapter.
st
• wxPython: This is an open-source Python
ty
si
interface for wxWindows
er
• JPython: JPython is a Python port for Java
iv
un
in
create GUI applications. Tkinter provides a powerful object-
y.
oriented interface to the Tk GUI toolkit.
ud
• Creating a GUI application using Tkinter is an easy task. All
st
you need to do is perform the following steps −
ty
– Import the Tkinter module.
si
– Create the GUI application main window.
er
– Add one or more of the above-mentioned widgets to the GUI
iv
application.
un
– Enter the main event loop to take action against each event
triggered by the user.
• Program1
in
#!/usr/bin/python
y.
ud
import Tkinter
st
top = Tkinter.Tk()
ty
# Code to add widgets will go here...
si
top.mainloop()
er
iv
un
Tkinter Widgets
• Tkinter provides various controls, such as
in
buttons, labels and text boxes used in a GUI
y.
ud
application. These controls are commonly
st
called widgets.
ty
• There are currently 15 types of widgets in
er
si
Tkinter. We present these widgets as well as a
iv
un
Frame The Frame widget is used as a container widget to organize other widgets.
in
Label The Label widget is used to provide a single-line caption for other widgets. It can also contain
images.
y.
Listbox The Listbox widget is used to provide a list of options to a user.
ud
Menubutton The Menubutton widget is used to display menus in your application.
Menu The Menu widget is used to provide various commands to a user. These commands are contained
st
inside Menubutton.
Message The Message widget is used to display multiline text fields for accepting values from a user.
ty
si
Radiobutton The Radiobutton widget is used to display a number of options as radio buttons. The user can
select only one option at a time.
er
Scale The Scale widget is used to provide a slider widget.
iv
Scrollbar The Scrollbar widget is used to add scrolling capability to various widgets, such as list boxes.
un
in
attributes.such as sizes, colors and fonts are
y.
specified.
ud
– Dimensions
st
–
ty
Colors
– Fonts
si
er
– Anchors
iv
un
– Relief styles
– Bitmaps
– Cursors
un
iv
er
si
ty
st
ud
Dimensions
y.
in
un
iv
er
si
ty
st
Fonts
ud
y.
in
un
iv
er
si
ty
st
ud
y.
Relief Styles
in
un
iv
er
si
ty
st
ud
BIT MAPS
y.
in
Geometry Management
in
organizing widgets throughout the parent widget area.
y.
Tkinter exposes the following geometry manager
ud
classes: pack, grid, and place.
st
– The pack() Method - This geometry manager organizes
ty
widgets in blocks before placing them in the parent
widget. si
er
– The grid() Method - This geometry manager organizes
iv
in
y.
ud
import Tkinter
import tkMessageBox
st
top = Tkinter.Tk()
ty
def helloCallBack():
si
tkMessageBox.showinfo( "Hello Python", "Hello World")
er
B = Tkinter.Button(top, text ="Hello", command = helloCallBack)
iv
B.pack()
un
top.mainloop()
Canvas
• The Canvas is a rectangular area intended for
drawing pictures or other complex layouts.
in
You can place graphics, text, widgets or frames
y.
ud
on a Canvas. import Tkinter
st
import tkMessageBox
• program3
ty
si
top = Tkinter.Tk()
er
iv
C = Tkinter.Canvas(top, bg="blue", height=250,
width=300)
un
in
y.
ud
from Tkinter import *
st
top = Tk()
ty
L1 = Label(top, text="User Name")
si
L1.pack( side = LEFT)
er
E1 = Entry(top, bd =5)
iv
E1.pack(side = RIGHT)
un
top.mainloop()
Frame
from Tkinter import *
root = Tk()
• program5 frame = Frame(root)
in
frame.pack()
y.
bottomframe = Frame(root)
ud
bottomframe.pack( side = BOTTOM )
redbutton = Button(frame, text="Red", fg="red")
st
redbutton.pack( side = LEFT)
ty
si
greenbutton = Button(frame, text="Brown", fg="brown")
er
greenbutton.pack( side = LEFT )
iv
un
root.mainloop()
Listbox
from Tkinter import *
• Program6 import tkMessageBox
in
import Tkinter
y.
ud
top = Tk()
st
Lb1 = Listbox(top)
ty
Lb1.insert(1, "Python")
si
Lb1.insert(2, "Perl")
er
Lb1.insert(3, "C")
iv
Lb1.insert(4, "PHP")
un
Lb1.insert(5, "JSP")
Lb1.insert(6, "Ruby")
Lb1.pack()
top.mainloop()
Radiobutton
from Tkinter import *
• program7
def sel():
selection = "You selected the option " + str(var.get())
in
label.config(text = selection)
y.
root = Tk()
ud
var = IntVar()
R1 = Radiobutton(root, text="Option 1", variable=var,
st
value=1, command=sel)
R1.pack( anchor = W )
ty
si
er R2 = Radiobutton(root, text="Option 2", variable=var,
value=2, command=sel)
R2.pack( anchor = W )
iv
un
label = Label(root)
label.pack()
root.mainloop()
Menubutton
from Tkinter import *
import tkMessageBox
• program8 import Tkinter
top = Tk()
in
mb= Menubutton ( top, text="condiments“,relief=RAISED )
y.
mb.grid()
ud
mb.menu = Menu ( mb, tearoff = 0 )
mb["menu"] = mb.menu
st
ty
mayoVar = IntVar()
si
ketchVar = IntVar()
er
iv
mb.menu.add_checkbutton ( label="mayo",
un
variable=mayoVar )
mb.menu.add_checkbutton ( label="ketchup",
variable=ketchVar )
mb.pack()
top.mainloop()
Check button
from Tkinter import *
import tkMessageBox
program9 import Tkinter
in
y.
top = Tkinter.Tk()
ud
CheckVar1 = IntVar()
CheckVar2 = IntVar()
st
C1 = Checkbutton(top, text = "Music", variable = CheckVar1,
ty
onvalue = 1, offvalue = 0, height=5, width = 20)
si
C2 = Checkbutton(top, text = "Video", variable = CheckVar2,
er
onvalue = 1, offvalue = 0, height=15, width = 50)
iv
C1.pack()
un
C2.pack()
top.mainloop()
Bring Image
# Putting a gif image on a canvas with Tkinter
in
# create the canvas, size in pixels
y.
canvas = Canvas(width = 300, height = 200, bg = 'yellow')
ud
# pack the canvas into a frame/form
canvas.pack(expand = YES, fill = BOTH)
st
# load the .gif image file
ty
# put in your own gif file here, may need to add full path
si
gif1 = PhotoImage(file = 'dw.gif')
er
# put gif image on canvas
iv
# pic's upper left corner (NW) on the canvas is at x=50 y=10
un