GUI Programming in Python Using Tkinter
GUI Programming in Python Using Tkinter
GUI Programming in Python Using Tkinter
using Tkinter
4. Cursors (arrow,"circle,"clock,"cross)
Ex: from Tkinter import *
import Tkinter top = Tkinter.Tk()
B1 = Tkinter.Button(top, text ="circle", relief=RAISED,\ cursor="circle")
B2 = Tkinter.Button(top, text ="plus", relief=RAISED,\ cursor="plus")
B1.pack()
B2.pack()
top.mainloop()
Geometry Management
1. Geometry Manager Classes:
a. The pack() Method This geometry manager
organizes widgets in blocks before placing them
in the parent widget.
b. The grid() Method This geometry manager
organizes widgets in a table-like structure in the
parent widget.
c. The place() Method This geometry manager
organizes widgets by placing them in a specific
position in the parent widget.
Geometry Management PACK()
widget.pack( pack_options )
-expand When set to true, widget expands to
fill any space not otherwise used in widget's
parent.
fill Determines whether widget fills any extra
space allocated to it by the packer, or keeps its
own minimal dimensions: NONE (default), X (fill
only horizontally), Y (fill only vertically), or BOTH
(fill both horizontally and vertically).
side Determines which side of the parent
widget packs against: TOP (default), BOTTOM,
LEFT, or RIGHT.
Geometry Management PACK()
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()
Geometry Management PACK()
Placing a number of widgets on top of each other
To put a number of widgets in a column, you can use
the pack method without any options:
from tkinter import *
root = Tk()
w = Label(root, text="Red", bg="red,fg="white")
w.pack()
w=Label(root, text="Green", bg="green", fg="black")
w.pack()
w = Label(root, text="Blue", bg="blue", fg="white")
w.pack()
mainloop()
Geometry Management PACK()
You can use the fill=X option to make all widgets as
wide as the parent widget:
from tkinter import *
root = Tk()
w = Label(root, text="Red", bg="red", fg="white")
w.pack(fill=X)
w= Label(root, text="Green", bg="green", fg="black")
w.pack(fill=X)
w = Label(root, text="Blue", bg="blue", fg="white")
w.pack(fill=X)
mainloop()
Geometry Management PACK()
Placing a number of widgets side by side
To pack widgets side by side, use the side option. If you
wish to make the widgets as high as the parent, use the
fill=Y option too:
from tkinter import *
root = Tk()
w = Label(root, text="Red", bg="red", fg="white")
w.pack(side=LEFT)
w = Label(root, text="Green", bg="green", fg="black")
w.pack(side=LEFT)
w = Label(root, text="Blue", bg="blue", fg="white")
w.pack(side=LEFT)
mainloop()
Event Driven
Usually, widgets have some associated
behaviors, such as when a button is pressed, or
text is filled into a text field. These types of user
behaviors are called events, and the GUIs
response to such events are known as callbacks.
Events can include the actual button press (and
release), mouse movement, hitting the Return
or Enter key, etc. The entire system of events
that occurs from the beginning until the end of a
GUI application is what drives it. This is known
as event-driven processing
Event Handling
Event sources (widgets) can specify their handlers
command handlers - use the 'command=' keyword
followed by the command you want executed
callbacks
A callback is the name of the function that is to be run in
response of an event
Callbacks can be defined as a free standing function in our
program or as a class member
Ex1.
from tkinter import *
root = Tk()
Button (root, text='Press Me', command=root.quit).pack(side=LEFT)
root.mainloop()
Event Handling
from tkinter import *
def quit():
print 'Hello, getting out of here'
import sys; sys.exit()
widget = Button(None, text='Press me to quit' ,
command=quit)
widget.pack()
widget.mainloop()
Bound Method Callbacks
from tkinter import *
class HelloClass:
# create the window in the class constructor
def __init__(self):
widget = Button(None, text='Press Me to quit',
command=self.quit)
widget.pack()
def quit(self):
print ('leaving now)
import sys ; sys.exit()
HelloClass() # create a HelloClass object
mainloop()
Binding Event
from tkinter import *
def hello(event):
print 'Double click to exit'
def quit(event):
print 'caught a double click, leaving'
import sys ; sys.exit()
widget = Button(None, text='Hello Event World')
widget.pack()
widget.bind(<Button-1>', hello)
widget.bind(<Double-1>' , quit)
widget.mainloop()
Tkinter Widgets
Tkinter Widgets
1. Button: Button(master=None, **options).
2. Label(master=None, **options)
Program that toggle the button background color
import Tkinter as tk button1 = tk.Button(frame1, compound=tk.TOP, width=148,
button_flag = True height=240, image=photo1,
def click(): text="optional text", bg='green', command=click)
"""
.respond to the button click button1.pack(side=tk.LEFT, padx=2, pady=2)
# save the button's image from garbage collection (needed?)
""" button1.image = photo1
global button_flag # start the event loop
# toggle button colors as a test root.mainloop()
if button_flag:
button1.config(bg="white")
button_flag = False
else:
button1.config(bg="green")
button_flag = True
root = tk.Tk()
# create a frame and pack it
frame1 = tk.Frame(root)
frame1.pack(side=tk.TOP, fill=tk.X)
# pick a (small) image file you have in the working directory ...
photo1 = tk.PhotoImage(file="rpi.gif")
# create the image button, image is above (top) the optional text
Widget Configuration
Configuration can be specified at creation time or changed as the program is running by using
the config method
import Tkinter
top = Tkinter.Tk()
w = Tkinter.Label(top, text="Hello, world!")
w.pack()
w.config(text="Hello Jim!")
Tkinter.mainloop()
Updating the text variable
from Tkinter import *
root = Tk()
# To create a Tkinter variable, call the corresponding constructor
v = StringVar()
label = Label(root, textvariable = v).pack()
v.set("New Text!")
print v.get()
root.mainloop()
Label and Button Demo
import tkinter
top = tkinter.Tk()
hello = tkinter.Label(top, text='Hello World!')
hello.pack()
quit = tkinter.Button(top, text='QUIT', command=top.quit,
bg='red', fg='white')
quit.pack(fill=Tkinter.X, expand=1)
tkinter.mainloop()
Pack Geometry Manager
from tkinter import *
root = Tk()
w = Label(root, text="Red", bg="red", fg="white")
w.pack()
w = Label(root, text="Green", bg="green", fg="black")
w.pack()
w = Label(root, text="Blue", bg="blue", fg="white")
w.pack()
mainloop()
def add():
m1.config(text=str(int(firstno.get())+int(secondno.get())))
top=Tk()
top.title("Adding two Numbers")
l1=Label(top,text="Enter the First No")
l1.grid(row=0,column=0)
firstno=Entry(top)
firstno.grid(row=0,column=1)
m1=Message(top)
m1.grid(row=2,column=1)
top.mainloop()
Adding Menu Example
from tkinter import *
import sys
def doNothing():
print("Test")
root = Tk()
root.title("TextEditor")
root.geometry("300x200")
menu = Menu(root)
root.config(menu=menu)
subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Project...", command =doNothing)
subMenu.add_command(label="Save", command=doNothing)
subMenu.add_separator()
editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Undo",command=doNothing)
root.mainloop()