Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
62 views

GUI Using Python

tkinter is the standard GUI library for Python. It provides widgets like buttons, labels, text boxes etc. to build graphical user interfaces. The key steps for tkinter programming are to import tkinter, create a main window, add widgets to it and start the main event loop. Widgets are organized using geometry managers like pack, grid and place which control widget positioning and layout. Common widgets include buttons, labels, entries, frames, listboxes, checkbuttons and menus. Images can also be added and widgets support configuration of colors, sizes etc.

Uploaded by

Vamsi Dandu
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

GUI Using Python

tkinter is the standard GUI library for Python. It provides widgets like buttons, labels, text boxes etc. to build graphical user interfaces. The key steps for tkinter programming are to import tkinter, create a main window, add widgets to it and start the main event loop. Widgets are organized using geometry managers like pack, grid and place which control widget positioning and layout. Common widgets include buttons, labels, entries, frames, listboxes, checkbuttons and menus. Images can also be added and widgets support configuration of colors, sizes etc.

Uploaded by

Vamsi Dandu
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

GUI Using Python

tkinter Programming

• tkinter is the standard GUI library for Python.

• provides a fast and easy way to create GUI


applications.
• tkinter provides a powerful object-oriented
interface to the Tk GUI toolkit.
Steps for GUI-tkinter Programming

– Import the tkinter module.

– Create the GUI application main window.

– Add one or more widgets to the GUI application.

– Enter the main event loop to take action against


each event triggered by the user.
from tkinter import * # importing tkinter module
top = tkinter.Tk() # creating Tk window
# Code to add widgets will go here...
top.mainloop() # Execute Tkinter

top.geometry('250x150')
tkinter Widgets
• tkinter provides various controls, such as
buttons, labels and text boxes used in a GUI
application. These controls are commonly
called widgets.
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.
Label The Label widget is used to provide a single-line caption for other
widgets. It can also contain images.
Listbox The Listbox widget is used to provide a list of options to a user.
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 inside Menubutton.
Operator Description
Message The Message widget is used to display multiline text fields for
accepting values from a user.
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.
Scale The Scale widget is used to provide a slider widget.
Scrollbar The Scrollbar widget is used to add scrolling capability to various
widgets, such as list boxes.
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.
MessageBox This module is used to display message boxes in your
applications.
Geometry Management
• All tkinter widgets have access to specific geometry
management methods, which have the purpose of
organizing widgets throughout the parent widget area.
• geometry manager classes: pack, grid, and place.
– The pack() Method - This geometry manager organizes
widgets in blocks before placing them in the parent widget.
– The grid() Method - This geometry manager organizes
widgets in a table-like structure in the parent widget.
– The place() Method -This geometry manager organizes
widgets by placing them in a specific position in the parent
widget.
Pack geometry manager packs widgets relative to the earlier widget

tkinter packs all the widgets one after the other in a window

options like fill, expand, and side to control pack geometry manager

•Put a widget inside a frame (or any other container widget),


and have it fill the entire frame
•Place a number of widgets on top of each other
•Place a number of widgets side by side
Button

from tkinter import *


from tkinter import messagebox
top = Tk()
def helloCallBack():
messagebox.showinfo( "Hello Python", "Hello World")
B = Button(top, text ="Hello", command = helloCallBack)
B.pack()
top.mainloop()
Code #1: Putting a widget inside frame and filling entire frame. We can do this with
the help of expand and fill options.

from tkinter import *

master = Tk()

# creating a Frame which can expand according to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)

# button widgets which can also expand and fill in the parent widget entirely
b1 = Button(pane, text = "Click me !")
b1.pack(fill = BOTH, expand = True)

# Button 2
b2 = Button(pane, text = "Click me too")
b2.pack(fill = BOTH, expand = True)

master.mainloop()
Code #2: Placing widgets on top of each other

from tkinter import *


master = Tk()

pane = Frame(master)
pane.pack(fill = BOTH, expand = True)

b1 = Button(pane, text = "Click me !", background = "red", fg = "white")


b1.pack(side = TOP, expand = True, fill = BOTH)

b2 = Button(pane, text = "Click me too",background = "blue", fg = "white")


b2.pack(side = TOP, expand = True, fill = BOTH)

b3 = Button(pane, text = "I'm also button", background = "green", fg = "white")


b3.pack(side = TOP, expand = True, fill = BOTH)

master.mainloop()
Code #3: Placing widgets side by side.

side = LEFT

side = RIGHT
side = BOTTOM
Canvas
• The Canvas is a rectangular area intended for drawing
pictures or other complex layouts. You can place graphics,
text, widgets or frames on a Canvas.

from tkinter import *


top = Tk()
C = Canvas(top, bg="blue", height=250, width=300)
coord = 10, 50, 240, 210
arc = C.create_arc(coord, start=0, extent=150, fill="red")
C.pack()
top.mainloop()
C = Canvas(root, height, width, bd, bg, ..)
• root = root window.
• height = height of the canvas widget.
• width = width of the canvas widget.
• bg = background colour for canvas.
• bd = border of the canvas window.
• scrollregion (w, n, e, s)tuple defined as a region for scrolling left, top,
bottom and right.
• highlightcolor colour shown in the focus highlight.
• cursor It can be defined as a cursor for the canvas which can be a
circle, a do, an arrow etc.
• confine decides if canvas can be accessed outside the scroll region.
• relief type of the border which can be SUNKEN, RAISED, GROOVE and
RIDGE. 
Creating shapes

• oval = C.create_oval(x0, y0, x1, y1, options)

• arc = C.create_arc(20, 50, 190, 240, start=0, extent=110, fill="red")

• line = C.create_line(x0, y0, x1, y1, ..., xn, yn, options)


Entry

from tkinter import *


top = Tk()
L1 = Label(top, text="User Name")
L1.pack( side = LEFT)
E1 = Entry(top, bd =5)
E1.pack(side = RIGHT)
top.mainloop()
Frame
from tkinter import *
root = Tk()
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
redbutton = Button(frame, text="Red", fg="red")
redbutton.pack( side = LEFT)

greenbutton = Button(frame, text="Brown", fg="brown")


greenbutton.pack( side = LEFT )

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 *


root = Tk()

Lb1 = Listbox(root)
Lb1.insert(1, "Python")
Lb1.insert(2, "Perl")
Lb1.insert(3, "C")
Lb1.insert(4, "PHP")
Lb1.insert(5, "JSP")
Lb1.insert(6, "Ruby")
Lb1.pack()

root.mainloop()
Radiobutton
from tkinter import *
root = Tk()
def sel():
selection = "You selected the option " + str(var.get())
label.config(text = selection)

var = IntVar()
R1 = Radiobutton(root, text="Option 1", variable=var, value=1, command=sel)
R1.pack( anchor = W )

R2 = Radiobutton(root, text="Option 2", variable=var, value=2, command=sel)


R2.pack( anchor = W )

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 *


top = Tk()
mb= Menubutton ( top, text="condiments",relief=RAISED )
mb.grid()
mb.menu = Menu ( mb, tearoff = 0 )
mb["menu"] = mb.menu

mayoVar = IntVar()
ketchVar = IntVar()

mb.menu.add_checkbutton ( label="mayo", variable=mayoVar )


mb.menu.add_checkbutton ( label="ketchup", variable=ketchVar )

mb.pack()

top.mainloop()
Check button
from tkinter import *
top = Tk()

CheckVar1 = IntVar()
CheckVar2 = IntVar()

C1 = Checkbutton(top, text = "Music", variable = CheckVar1, onvalue = 1, offvalue =


0, height=5, width = 20)
C2 = Checkbutton(top, text = "Video", variable = CheckVar2, onvalue = 1, offvalue = 0,
height=5, width = 20)

C1.pack()
C2.pack()

top.mainloop()
Images

from tkinter import *


top = Tk()

canvas = Canvas(width = 300, height = 200, bg = 'yellow')

canvas.pack(expand = YES, fill = BOTH)


gif1 = PhotoImage(file = ‘dw.png')
canvas.create_image(50, 10, image = gif1, anchor = NW)

top.mainloop()
Grid()

• Grid geometry manager puts the widgets in a 2-D table


• The master widget is split into a number of rows and columns
• Each “cell” in the resulting table can hold a widget.
• Most flexible geometry managers in Tkinter
from tkinter import *

master = Tk()

l1 = Label(master, text = "First:")


l2 = Label(master, text = "Second:")

l1.grid(row = 0, column = 0, sticky = W, pady = 2)


l2.grid(row = 1, column = 0, sticky = W, pady = 2)

e1 = Entry(master)
e2 = Entry(master)

e1.grid(row = 0, column = 1, pady = 2)


e2.grid(row = 1, column = 1, pady = 2)

mainloop()
from tkinter import *
master = Tk()

l1 = Label(master, text = "Height")


l2 = Label(master, text = "Width")

l1.grid(row = 0, column = 0, sticky = W, pady = 2)


l2.grid(row = 1, column = 0, sticky = W, pady = 2)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row = 0, column = 1, pady = 2)
e2.grid(row = 1, column = 1, pady = 2)
c1 = Checkbutton(master, text = "Preserve")
c1.grid(row = 2, column = 0, sticky = W, columnspan = 2)
img = PhotoImage(file = r'download.png')
img1 = img.subsample(2, 2)
Label(master, image = img1).grid(row = 0, column = 2,
columnspan = 2, rowspan = 2, padx = 5, pady = 5)
b1 = Button(master, text = "Zoom in")
b2 = Button(master, text = "Zoom out")
b1.grid(row = 2, column = 2, sticky = E)
b2.grid(row = 2, column = 3, sticky = E)
mainloop()
Spinbox

from tkinter import *

root = Tk()
root.geometry("300x200")

w = Label(root, text ='I am a spinbox', font = "50")


w.pack()

sp = Spinbox(root, from_= 0, to = 20)


sp.pack()

root.mainloop()
Scale
from tkinter import *

root = Tk()
root.geometry("400x300")

v1 = DoubleVar()

def show1():
sel = "Horizontal Scale Value = " + str(v1.get())
l1.config(text = sel, font =("Courier", 14))
s1 = Scale( root, variable = v1,from_ = 1, to = 100, orient = HORIZONTAL)
l3 = Label(root, text = "Horizontal Scaler")
b1 = Button(root, text ="Display Horizontal", command = show1,bg = "yellow")
l1 = Label(root)

s1.pack(anchor = CENTER)
l3.pack()
b1.pack(anchor = CENTER)
l1.pack()

root.mainloop()
Message
• This widget provides a multiline and
noneditable object that displays texts,
automatically breaking lines and justifying
their contents.
• Its functionality is very similar to the one
provided by the Label widget, except that it
can also automatically wrap the text,
maintaining a given width or aspect ratio.

You might also like