GUI Using Python
GUI Using Python
tkinter Programming
– Import the tkinter module.
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
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
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)
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.
root.mainloop()
Listbox
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 )
label = Label(root)
label.pack()
root.mainloop()
Menubutton
mayoVar = IntVar()
ketchVar = IntVar()
mb.pack()
top.mainloop()
Check button
from tkinter import *
top = Tk()
CheckVar1 = IntVar()
CheckVar2 = IntVar()
C1.pack()
C2.pack()
top.mainloop()
Images
top.mainloop()
Grid()
master = Tk()
e1 = Entry(master)
e2 = Entry(master)
mainloop()
from tkinter import *
master = Tk()
root = Tk()
root.geometry("300x200")
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.