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

Building GUI Using Python

Uploaded by

Jatin Verma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Building GUI Using Python

Uploaded by

Jatin Verma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

un

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

which gives Python scripts seamless access to


Java class libraries on the local machine
Tkinter Programming

• Tkinter is the standard GUI library for Python. Python when


combined with Tkinter provides a fast and easy way to

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

brief description in the following table −


Operator Description
Button The Button widget is used to display buttons in your application.
Canvas The Canvas widget is used to draw shapes, such as lines, ovals, polygons and rectangles, in your
application.
Checkbutton The Checkbutton widget is used to display a number of options as checkboxes. The user can select
multiple options at a time.
Entry The Entry widget is used to display a single-line text field for accepting values from a user.

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

Text The Text widget is used to display text in multiple lines.


Toplevel The Toplevel widget is used to provide a separate window container.
Spinbox The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select
from a fixed number of values.
PanedWindow A PanedWindow is a container widget that may contain any number of panes, arranged
horizontally or vertically.
LabelFrame A labelframe is a simple container widget. Its primary purpose is to act as a spacer or container for
complex window layouts.
tkMessageBox This module is used to display message boxes in your applications.
Standard attributes
• Let us take a look at how some of their common

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

• All Tkinter widgets have access to specific geometry


management methods, which have the purpose of

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

widgets in a table-like structure in the parent widget.


un

– The place() Method -This geometry manager organizes


widgets by placing them in a specific position in the parent
widget.
Button
• Program2

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

coord = 10, 50, 240, 210


arc = C.create_arc(coord, start=0, extent=150, fill="red")
C.pack()
top.mainloop()
un
iv
er
si
ty
st
ud
y.
in
Entry
• program4

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

bluebutton = Button(frame, text="Blue", fg="blue")


bluebutton.pack( side = LEFT )

blackbutton = Button(bottomframe, text="Black", fg="black")


blackbutton.pack( side = BOTTOM)

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

R3 = Radiobutton(root, text="Option 3", variable=var,


value=3, command=sel)
R3.pack( anchor = W)

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

Program from Tkinter import *


root=Tk()

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

canvas.create_image(50, 10, image = gif1, anchor = NW)


# run it ...
root.mainloop()
un
iv
er
si
ty
st
ud
y.
in

You might also like