Python GUI Programming
Python - GUI Programming (Tkinter)
Python provides various options for developing graphical user interfaces (GUIs). Most important are listed below:
Tkinter Programming:
import tkinter
top = tkinter.Tk()
# Code to add widgets will go here...
top.mainloop()
Python - Tkinter Button
The Button widget is used to add buttons in a Python application. These buttons can display text or images that convey the purpose of the buttons. You can attach a function or a method to a button, which is called automatically when you click the button.
Syntax:
w = Button ( master, option=value, ... )
Parameters:
Example:
import tkinter
from tkinter import messagebox
top = tkinter.Tk()
def helloCallBack():
messagebox.showinfo( ”Title", "Hello there")
B = tkinter.Button(top, text ="Hello", command = helloCallBack)
B.pack() # optional
top.mainloop()
Python - Tkinter 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.
Syntax:
w = Canvas ( master, option=value, ... )
The Canvas widget can support the following standard items:
coord = 10, 50, 240, 210
arc = canvas.create_arc(coord, start=0, extent=150, fill="blue")
filename = PhotoImage(file = "sunshine.gif")
image = canvas.create_image(50, 50, anchor=NE, image=filename)
line = canvas.create_line(x0, y0, x1, y1, ..., xn, yn, options)
oval = canvas.create_polygon(x0, y0, x1, y1,...xn, yn, options)
Example:
import tkinter
from tkinter import messagebox
From tkinter import Canvas
top = tkinter.Tk()
C = tkinter.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()
Example:
import Tkinter
import tkMessageBox
top = Tkinter.Tk()
C = Tkinter.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()
Python - Tkinter Checkbutton
The Checkbutton widget is used to display a number of options to a user as toggle buttons. The user can then select one or more options by clicking the button corresponding to each option.
You can also display images in place of text.
Syntax:
w = Checkbutton ( master, option, ... )
Example:
from tkinter import *
top = tkinter.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()
Python - Tkinter Entry:
Syntax:
Here is the simple syntax to create this widget:
w = Entry( master, option, ... )
Parameters:
Example:
From tkinter import *
top = tkinter.Tk()
L1 = Label(top, text="User Name")
L1.pack( side = LEFT)
E1 = Entry(top, bd =5)
E1.pack(side = CENTER)
top.mainloop()
Python - Tkinter Frame
Syntax:
Here is the simple syntax to create this widget:
w = Frame ( master, option, ... )
Parameters:
Example:
from tkinter import *
import tkinter
root = tkinter.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()
Python - Tkinter Label
Syntax:
Here is the simple syntax to create this widget:
w = Label ( master, option, ... )
Parameters:
Example:
From tkinter import *
root = tkinter.Tk()
var = StringVar()
label = Label(root, textvariable=var, relief=RAISED )
var.set("Hey!? How are you doing?")
label.pack()
root.mainloop()
Python - Tkinter Listbox
Syntax:
w = Listbox ( master, option, ... )
Parameters:
Example:
From tkinter import *
Import tkinter
top = tkinter.Tk()
Lb1 = Listbox(top)
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()
top.mainloop()
Python - Tkinter Menubutton
Syntax:
Here is the simple syntax to create this widget:
w = Menubutton ( master, option, ... )
Parameters:
Example:
from tkinter import *
import tkinter
top = tkinter.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()
Python - Tkinter Message
Syntax:
Here is the simple syntax to create this widget:
w = Message ( master, option, ... )
Parameters:
Example:
from tkinter import *
import tkinter
root = tkinter.Tk()
var = StringVar()
label = Message( root, textvariable=var, relief=RAISED )
var.set("Hey!? How are you doing?")
label.pack()
root.mainloop()
Python - Tkinter Radiobutton
Syntax:
Here is the simple syntax to create this widget:
w = Radiobutton ( master, option, ... )
Parameters:
Example:
from tkinter import *
import tkinter
def sel():
selection = "You selected the option " + str(var.get())
label.config(text = selection)
root = Tk()
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 )
label = Label(root)
label.pack()
root.mainloop()
Python - Tkinter Scale
Syntax:
Here is the simple syntax to create this widget:
w = Scale ( master, option, ... )
Parameters:
Example:
from tkinter import *
import tkinter
def sel():
selection = "Value = " + str(var.get())
label.config(text = selection)
root = Tk()
var = DoubleVar()
scale = Scale( root, variable = var )
scale.pack(anchor=CENTER)
button = Button(root, text="Get Scale Value", command=sel)
button.pack(anchor=CENTER)
label = Label(root)
label.pack()
root.mainloop()
Python - Tkinter Scrollbar
Syntax:
Here is the simple syntax to create this widget:
w = Scrollbar ( master, option, ... )
Parameters:
Example:
from tkinter import *
import tkinter
root = Tk()
scrollbar = Scrollbar(root)
scrollbar.pack( side = RIGHT, fill=Y )
mylist = Listbox(root, yscrollcommand = scrollbar.set )
for line in range(100):
mylist.insert(END, "This is line number " + str(line))
mylist.pack( side = LEFT, fill = BOTH )
scrollbar.config( command = mylist.yview )
mainloop()
Python - Tkinter Text
Syntax:
Here is the simple syntax to create this widget:
w = Text ( master, option, ... )
Parameters:
Example:
from tkinter import *
import tkinter
def onclick():
pass
root = Tk()
text = Text(root)
text.insert(INSERT, "Hello.....")
text.insert(END, "Bye Bye.....")
text.pack()
text.tag_add("here", "1.0", "1.4")
text.tag_add("start", "1.8", "1.13")
text.tag_config("here", background="yellow", foreground=”red")
text.tag_config("start", background="black", foreground="green")
root.mainloop()
Python - Tkinter Toplevel
Syntax:
Here is the simple syntax to create this widget:
w = Toplevel ( option, ... )
Parameters:
Example:
from tkinter import *
import tkinter
root = Tk()
top = Toplevel()
top.mainloop()
Python - Tkinter Spinbox
Syntax:
Here is the simple syntax to create this widget:
w = Spinbox( master, option, ... )
Parameters:
Example:
from tkinter import *
import tkinter
master = Tk()
w = Spinbox(master, from_=0, to=10)
w.pack()
mainloop()
Python - Tkinter PanedWindow
Syntax:
Here is the simple syntax to create this widget:
w = PanedWindow( master, option, ... )
Parameters:
Example:
from tkinter import *
import tkinter
m1 = PanedWindow()
m1.pack(fill=BOTH, expand=1)
left = Label(m1, text="left pane")
m1.add(left)
m2 = PanedWindow(m1, orient=VERTICAL)
m1.add(m2)
top = Label(m2, text="top pane")
m2.add(top)
bottom = Label(m2, text="bottom pane")
m2.add(bottom)
mainloop()
Python - Tkinter LabelFrame
Syntax:
Here is the simple syntax to create this widget:
w = LabelFrame( master, option, ... )
Parameters:
Example:
from tkinter import *
root = Tk()
labelframe = LabelFrame(root, text="This is a LabelFrame")
labelframe.pack(fill="both", expand="yes")
left = Label(labelframe, text="Inside the LabelFrame")
left.pack()
root.mainloop()