Source Code
Source Code
import tkinter
top = tkinter.Tk()
# Code to add widgets will go here...
top.mainloop()
Tkinter Widgets
• Tkinter provides various controls, such as
buttons, labels and text boxes used in a GUI
application. These controls are commonly
called widgets.
• There are currently 15 types of widgets in
Tkinter. We present these widgets as well as a
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.
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.
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.
import tkinter
from tkinter import messagebox
top = tkinter.Tk()
def helloCallBack():
messageBox.showinfo( "Hello Python", "Hello World")
B = Tkinter.Button(top, text ="Hello", command = helloCallBack)
B.pack()
top.mainloop()
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. import tkinter
• program3 top = tkinter.Tk()
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()
• program5 frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
redbutton = Button(frame, text="Red", fg="red")
redbutton.pack( side = LEFT)
root.mainloop()
Listbox
• Program6 from tkinter import *
top = 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()
Radiobutton
from tkinter import *
• program7
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 )
label = Label(root)
label.pack()
root.mainloop()
Menubutton
from tkinter import *
def donothing():
• program8 print("nothing happes")
root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff = 1)
filemenu.add_command(label="New", command =
donothing)
filemenu.add_command(label = "Open", command =
donothing)
filemenu.add_command(label = "Save", command =
donothing)
filemenu.add_command(label = "Save as...", command =
donothing)
filemenu.add_command(label = "Close", command =
donothing)
filemenu.add_command(label = "Exit", command =
donothing)
menubar.add_cascade(label = "File", menu = filemenu)
root.config(menu= menubar)
root.mainloop()
Check button
from tkinter import messagebox
import tkinter
program9
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=15, width = 50)
C1.pack()
C2.pack()
top.mainloop()
Bring Image
# Putting a gif image on a canvas with Tkinter