Python Widgets (1)
Python Widgets (1)
BY:GAYATRI D. KAMTEKAR
Button
• To add a button in your application, this widget is used.
• The general syntax is:
w=Button(master, option=value)
• master is the parameter used to represent the parent window.
• There are number of options which are used to change the format of the Buttons. Number of options can be
passed as parameters separated by commas. Some of them are listed below.
• activebackground: to set the background color when button is under the cursor.
• activeforeground: to set the foreground color when button is under the cursor.
• bg: to set he normal background color.
• command: to call a function.
• font: to set the font on the button label.
• image: to set the image on the button.
• width: to set the width of the button.
• height: to set the height of the button.
Button
Example: Output:
import tkinter as tk
r = tk.Tk()
r.title('Counting Seconds')
button = tk.Button(r, text='Stop', width=25,
command=r.destroy)
button.pack()
r.mainloop()
Canvas
• It is used to draw pictures and other complex layout like graphics, text and widgets.
• The general syntax is:
w = Canvas(master, option=value)
• master is the parameter used to represent the parent window.
• There are number of options which are used to change the format of the widget. Number of options can be
passed as parameters separated by commas. Some of them are listed below.
• bd: to set the border width in pixels.
• bg: to set the normal background color.
• cursor: to set the cursor used in the canvas.
• highlightcolor: to set the color shown in the focus highlight.
• width: to set the width of the widget.
• height: to set the height of the widget.
Canvas
from tkinter import *
top = Tk()
top.geometry("200x200")
#creating a simple canvas
c = Canvas(top,bg = "pink",height = "200",width = 200)
arc = c.create_arc((5,10,150,200),start = 0,extent = 150, fill= "white")
c.pack()
top.mainloop()
CheckButton
• To select any number of options by displaying a number of options to a user as toggle buttons. The general
syntax is:
w = CheckButton(master, option=value)
• There are number of options which are used to change the format of this widget. Number of options can be
passed as parameters separated by commas. Some of them are listed below.
• Title: To set the title of the widget.
• activebackground: to set the background color when widget is under the cursor.
• activeforeground: to set the foreground color when widget is under the cursor.
• bg: to set he normal background Steganography
• command: to call a function.
• font: to set the font on the button label.
• image: to set the image on the widget.
CheckButton
from tkinter import *
master = Tk()
var1 = IntVar()
Checkbutton(master, text='male',
variable=var1).grid(row=0, sticky=W)
var2 = IntVar()
Checkbutton(master, text='female',
variable=var2).grid(row=1, sticky=W)
mainloop()
Entry
• It is used to input the single line text entry from the user.. For multi-line text input, Text widget is used.
• The general syntax is:
w=Entry(master, option=value)
master is the parameter used to represent the parent window.
• There are number of options which are used to change the format of the widget. Number of options can be
passed as parameters separated by commas. Some of them are listed below.
• bd: to set the border width in pixels.
• bg: to set the normal background color.
• cursor: to set the cursor used.
• command: to call a function.
• highlightcolor: to set the color shown in the focus highlight.
• width: to set the width of the button.
• height: to set the height of the button.
Entry
from tkinter import *
top = Tk()
top.geometry("400x250")
name = Label(top, text = "Name").place(x = 30,y = 50)
email = Label(top, text = "Email").place(x = 30, y = 90)
password = Label(top, text = "Password").place(x = 30, y = 130)
sbmitbtn = Button(top, text = "Submit", activebackground = "pink",
activeforeground = "blue").place(x = 30, y = 170)
e1 = Entry(top).place(x = 80, y = 50)
e2 = Entry(top).place(x = 80, y = 90)
e3 = Entry(top).place(x = 95, y = 130)
top.mainloop()
Frame
• It acts as a container to hold the widgets. It is used for grouping and organizing the widgets.
• The general syntax is:
w = Frame(master, option=value)
• master is the parameter used to represent the parent window.
• There are number of options which are used to change the format of the widget. Number of options can be
passed as parameters separated by commas. Some of them are listed below.
• highlightcolor: To set the color of the focus highlight when widget has to be focused.
• bd: to set the border width in pixels.
• bg: to set the normal background color.
• cursor: to set the cursor used.
• width: to set the width of the widget.
• height: to set the height of the widget.
Frame
• from tkinter import *
• top = Tk()
• btn3.pack(side = LEFT)
• top.geometry("140x100")
• btn4 = Button(leftframe, text="Modify", fg="black",
• frame = Frame(top)
activebackground = "white")
• frame.pack()
• btn4.pack(side = RIGHT)
• leftframe = Frame(top)
• top.mainloop()
• leftframe.pack(side = LEFT)
• rightframe = Frame(top)
• rightframe.pack(side = RIGHT)
• btn1 = Button(frame, text="Submit",
fg="red",activebackground = "red")
• btn1.pack(side = LEFT)
• btn2 = Button(frame, text="Remove", fg="brown",
activebackground = "brown")
• btn2.pack(side = RIGHT)
• btn3 = Button(rightframe, text="Add", fg="blue",
activebackground = "blue")
Label
• It refers to the display box where you can put any text or image which can be updated any time as per the
code.
• The general syntax is:
w=Label(master, option=value)
• master is the parameter used to represent the parent window.
• There are number of options which are used to change the format of the widget. Number of options can be
passed as parameters separated by commas. Some of them are listed below.
• bg: to set he normal background color.
• bg to set he normal background color.
• command: to call a function.
• font: to set the font on the button label.
• image: to set the image on the button.
• width: to set the width of the button.
• height” to set the height of the button.
Label
from tkinter import *
root = Tk()
w = Label(root, text=‘helloooooo!')
w.pack()
root.mainloop()
Listbox
• It offers a list to the user from which the user can accept any number of options.
• The general syntax is:
w = Listbox(master, option=value)
• master is the parameter used to represent the parent window.
• There are number of options which are used to change the format of the widget. Number of options can be
passed as parameters separated by commas. Some of them are listed below.
• highlightcolor: To set the color of the focus highlight when widget has to be focused.
• bg: to set he normal background color.
• bd: to set the border width in pixels.
• font: to set the font on the button label.
• image: to set the image on the widget.
• width: to set the width of the widget.
• height: to set the height of the widget.
Listbox
from tkinter import *
top = Tk()
Lb = Listbox(top)
Lb.insert(1, 'Python')
Lb.insert(2, 'Java')
Lb.insert(3, 'C++')
Lb.insert(4, 'Any other')
Lb.pack()
top.mainloop()
Menubutton
• The Menubutton widget can be defined as the drop-down menu that is shown to the user all the time. It is used to
provide the user a option to select the appropriate choice exist within the application.
w = Menubutton(master, option=value)
• master is the parameter used to represent the parent window.
• There are number of options which are used to change the format of the widget. Number of options can be passed as
parameters separated by commas. Some of them are listed below.
• activebackground: To set the background when mouse is over the widget.
• activeforeground: To set the foreground when mouse is over the widget.
• bg: to set he normal background color.
• bd: to set the size of border around the indicator.
• cursor: To appear the cursor when the mouse over the menubutton.
• image: to set the image on the widget.
• width: to set the width of the widget.
• height: to set the height of the widget.
• highlightcolor: To set the color of the focus highlight when widget has to be focused.
Menubutton
• from tkinter import *
• top = Tk()
• top.geometry("200x250")
• menubutton = Menubutton(top, text = "Language", relief = FLAT)
• menubutton.grid()
• menubutton.menu = Menu(menubutton)
• menubutton["menu"]=menubutton.menu
• menubutton.menu.add_checkbutton(label = "Hindi", variable=IntVar())
• menubutton.menu.add_checkbutton(label = "English", variable = IntVar())
• menubutton.pack()
• top.mainloop()
Menu
• The Menu widget is used to create various types of menus (top level, pull down, and
pop up) in the python application.
• The top-level menus are the one which is displayed just under the title bar of the parent
window. We need to create a new instance of the Menu widget and add various
commands to it by using the add() method.
w = Menu(master, options)
• postcommand: The postcommand can be set to any of the function which is called when the mourse hovers
the menu.
• relief: The type of the border of the widget. The default type is RAISED.
• image: It is used to display an image on the menu.
• selectcolor: The color used to display the checkbutton or radiobutton when they are selected.
• tearoff: By default, the choices in the menu start taking place from position 1. If we set the tearoff = 1, then it
will start taking place from 0th position.
• title: Set this option to the title of the window if you want to change the title of the window.
• from tkinter import Toplevel, Button, Tk, Menu
• top = Tk()
• menubar = Menu(top)
• file = Menu(menubar, tearoff=0)
edit.add_command(label="Delete")
• file.add_command(label="New")
edit.add_command(label="Select All")
• file.add_command(label="Open")
menubar.add_cascade(label="Edit", menu=edit)
• file.add_command(label="Save")
help = Menu(menubar, tearoff=0)
• file.add_command(label="Save as...")
help.add_command(label="About")
• file.add_command(label="Close")
menubar.add_cascade(label="Help", menu=help)
• file.add_separator()
• file.add_command(label="Exit", command=top.quit)
top.config(menu=menubar)
msg.pack()
top.mainloop()
Message
from tkinter import *
main = Tk()
ourMessage ='This is our Message'
messageVar = Message(main, text = ourMessage)
messageVar.config(bg='lightgreen')
messageVar.pack( )
main.mainloop( )
Radiobutton
• The Radiobutton widget is used to implement one-of-many selection in the python application. It shows
multiple choices to the user out of which, the user can select only one out of them. We can associate different
methods with each of the Radiobutton.
w = Radiobutton(top, options)
• selectcolor: The color of the radio button when it is selected.
• selectimage:The image to be displayed on the radiobutton when it is selected.
• state: It represents the state of the radio button. The default state of the Radiobutton is NORMAL. However,
we can set this to DISABLED to make the radiobutton unresponsive.
• text: The text to be displayed on the radiobutton.
• textvariable: It is of String type that represents the text displayed by the widget.
• underline: The default value of this option is -1, however, we can set this option to the number of character
which is to be underlined.
• value: The value of each radiobutton is assigned to the control variable when it is turned on by the user.
Radiobutton
• from tkinter import *
• def selection():
• selection = "You selected the option " + str(radio.get())
• label.config(text = selection)
• top = Tk()
• top.geometry("300x150")
• radio = IntVar()
• lbl = Label(text = "Favourite programming language:")
• lbl.pack()
• R1 = Radiobutton(top, text="C", variable=radio, value=1, command=selection)
• R1.pack( anchor = W )
• R2 = Radiobutton(top, text="C++", variable=radio, value=2, command=selection)
• R2.pack( anchor = W )
• R3 = Radiobutton(top, text="Java", variable=radio, value=3, command=selection)
• R3.pack( anchor = W)
• label = Label(top)
• label.pack()
• top.mainloop()
Scale
• The Scale widget is used to implement the graphical slider to the python application so that the user can
slide through the range of values shown on the slider and select the one among them.
• We can control the minimum and maximum values along with the resolution of the scale. It provides an
alternative to the Entry widget when the user is forced to select only one value from the given range of
values.
w = Scale(top, options)
• orient :It can be set to horizontal or vertical depending upon the type of the scale.
• relief: It represents the type of the border. The default is FLAT.
• repeatdelay: This option tells the duration up to which the button is to be pressed before the slider starts
moving in that direction repeatedly. The default is 300 ms.
• resolution: It is set to the smallest change which is to be made to the scale value.
• showvalue: The value of the scale is shown in the text form by default. We can set this option to 0 to
suppress the label.
• sliderlength: It represents the length of the slider window along the length of the scale. The default is 30
pixels. However, we can change it to the appropriate value.
Scale
• from tkinter import *
• def select():
• sel = "Value = " + str(v.get())
• label.config(text = sel)
• top = Tk()
• top.geometry("200x100")
• v = DoubleVar()
• scale = Scale( top, variable = v, from_ = 1, to = 50, orient =
HORIZONTAL)
• scale.pack(anchor=CENTER)
• btn = Button(top, text="Value", command=select)
• btn.pack(anchor=CENTER)
• label = Label(top)
• label.pack()
• top.mainloop()
Scrollbar
• The scrollbar widget is used to scroll down the content of the other widgets like listbox, text, and canvas.
However, we can also create the horizontal scrollbars to the Entry widget.
w = Scrollbar(top, options)
jump: It is used to control the behavior of the scroll jump. If it set to 1, then the callback is called when the user
releases the mouse button.
orient: It can be set to HORIZONTAL or VERTICAL depending upon the orientation of the scrollbar.
repeatdelay: This option tells the duration up to which the button is to be pressed before the slider starts
moving in that direction repeatedly. The default is 300 ms.
repeatinterval: The default value of the repeat interval is 100.
takefocus: We can tab the focus through this widget by default. We can set this option to 0 if we don't want
this behavior.
troughcolor: It represents the color of the trough.
width: It represents the width of the scrollbar.
Scrollbar
from tkinter import *
top = Tk()
sb = Scrollbar(top)
sb.pack(side = RIGHT, fill = Y)
mylist = Listbox(top, yscrollcommand = sb.set )
for line in range(30):
mylist.insert(END, "Number " + str(line))
mylist.pack( side = LEFT )
sb.config( command = mylist.yview )
mainloop()
Text
• The Text widget is used to show the text data on the Python application. However, Tkinter provides
us the Entry widget which is used to implement the single line text box.
• The Text widget is used to display the multi-line formatted text with various styles and attributes. The
Text widget is mostly used to provide the text editor to the user.
w = Text(top, options)
• wrap: This option is used to wrap the wider lines into multiple lines. Set this option to the WORD to
wrap the lines after the word that fit into the available space. The default value is CHAR which breaks
the line which gets too wider at any character.
• xscrollcommand: To make the Text widget horizontally scrollable, we can set this option to the set()
method of Scrollbar widget.
• yscrollcommand: To make the Text widget vertically scrollable, we can set this option to the set()
method of Scrollbar widget.
• highlightcolor: To set the color of the focus highlight when widget has to be focused.
• insertbackground: To set the background of the widget.
Text
from tkinter import *
top = Tk()
text = Text(top)
text.insert(INSERT, "Name.....")
text.insert(END, "Salary.....")
text.pack()
text.tag_add("Write Here", "1.0", "1.4")
text.tag_add("Click Here", "1.8", "1.13")
text.tag_config("Write Here", background="yellow", foreground="black")
text.tag_config("Click Here", background="black", foreground="white")
top.mainloop()
Toplevel
• The Toplevel widget is used to create and display the toplevel windows which are directly managed by
the window manager. The toplevel widget may or may not have the parent window on the top of them.
• The toplevel widget is used when a python application needs to represent some extra information, pop-
up, or the group of widgets on the new window.
w = Toplevel(options)
• bg: to set he normal background color.
• bd: to set the size of border around the indicator.
• cursor: To appear the cursor when the mouse over the menubutton.
• width: to set the width of the widget.
• height: to set the height of the widget.
• class_ : The text selected in the text widget is exported to be selected to the window manager. We can
set this to 0 to make this behavior false.
Toplevel
from tkinter import *
root = Tk()
root.geometry("200x200")
def open():
top = Toplevel(root)
top.mainloop()
btn.place(x=75,y=50)
root.mainloop()
Spinbox
• The Spinbox widget is an alternative to the Entry widget. It provides the range of values to the user, out of
which, the user can select the one.
• It is used in the case where a user is given some fixed number of values to choose from.
w = Spinbox(top, options)
• bg: to set he normal background color.
• bd: to set the size of border around the indicator.
• cursor: To appear the cursor when the mouse over the menubutton.
• command: To call a function.
• width: to set the width of the widget.
• activebackground: To set the background when mouse is over the widget.
• disabledbackground: To disable the background when mouse is over the widget.
• from_: To set the value of one end of the range.
• to: To set the value of the other end of the range.
• validate: This option controls how the widget value is validated.
• validatecommand: It is associated to the function callback which is used for the
validation of the widget content.
Spinb
ox
from tkinter import *
top = Tk()
top.geometry("200x200")
spin = Spinbox(top, from_= 0, to = 25)
spin.pack()
top.mainloop()
PanedWindow
• The PanedWindow widget acts like a Container widget which contains one or more child widgets (panes)
arranged horizontally or vertically. The child panes can be resized by the user, by moving the separator lines
known as sashes by using the mouse.
• Each pane contains only one widget. The PanedWindow is used to implement the different layouts in the
python applications.
w= PanedWindow(master, options)
• bg: to set he normal background color.
• bd: to set the size of border around the indicator.
• cursor: To appear the cursor when the mouse over the menubutton.
• width: to set the width of the widget.
• height: to set the height of the widget.
PanedWindo
w
from tkinter import *
def add():
e2 = Entry(w2)
a = int(e1.get()) w2.add(e1)
b = int(e2.get()) w2.add(e2)
leftdata = str(a+b) bottom = Button(w2, text = "Add", command = add)
w2.add(bottom)
left.insert(1,leftdata) mainloop()
w1 = PanedWindow()
w1.pack(fill = BOTH, expand = 1)
left = Entry(w1, bd = 5)
w1.add(left)
w2 = PanedWindow(w1, orient = VERTICAL)
w1.add(w2)
e1 = Entry(w2)