This article is a list of all the Widgets in Tkinter.
Each widget in Tkinter is included here with a brief description regarding it’s use and purpose. This is followed by a code sample from it’s respective article and the output is shown through the use of an image or video.
Python Tkinter Widgets:
Frame:
Outlines the frame for your Tkinter window with a fixed size. Just like the human skeleton, a Tkinter window requires a frame to support it and give it structure.
from tkinter import *
root = Tk()
root.geometry("200x150")
frame = Frame(root)
frame.pack()
leftframe = Frame(root)
leftframe.pack(side=LEFT)
rightframe = Frame(root)
rightframe.pack(side=RIGHT)
label = Label(frame, text = "Hello world")
label.pack()
button1 = Button(leftframe, text = "Button1")
button1.pack(padx = 3, pady = 3)
button2 = Button(rightframe, text = "Button2")
button2.pack(padx = 3, pady = 3)
button3 = Button(leftframe, text = "Button3")
button3.pack(padx = 3, pady = 3)
root.title("Test")
root.mainloop()
Buttons:
The Python Tkinter Button is a standard Tkinter widget. A button is used as a way for the user to interact with the User interface. Once the button is clicked, an action is triggered by the program.
from tkinter import *
def set():
print("Hello World")
root = Tk()
root.geometry("200x150")
frame = Frame(root)
frame.pack()
button = Button(frame, text = "Button1", command = set,
fg = "red", font = "Verdana 14 underline",
bd = 2, bg = "light blue", relief = "groove")
button.pack(pady = 5)
root.mainloop()
Entry:
A standard Tkinter widget used to take input from the user through the user interface. A simple box is provided where the user can input text.
from tkinter import *
root = Tk()
root.geometry("200x150")
frame = Frame(root)
frame.pack()
my_entry = Entry(frame, width = 20)
my_entry.insert(0,'Username')
my_entry.pack(padx = 5, pady = 5)
my_entry2 = Entry(frame, width = 15)
my_entry2.insert(0,'password')
my_entry2.pack(padx = 5, pady = 5)
root.mainloop()
Check Button:
A check button is a Tkinter GUI widget that presents to the user a set of predefined options. The user may select more than one option.
from tkinter import *
root = Tk()
root.geometry("200x150")
frame = Frame(root)
frame.pack()
Var1 = IntVar()
Var2 = IntVar()
ChkBttn = Checkbutton(frame, width = 15, variable = Var1)
ChkBttn.pack(padx = 5, pady = 5)
ChkBttn2 = Checkbutton(frame, width = 15, variable = Var2)
ChkBttn2.pack(padx = 5, pady = 5)
Radio Button:
A radio button is a Tkinter GUI widget that allows the user to choose only one of a predefined set of mutually exclusive options.
from tkinter import *
root = Tk()
root.geometry("200x150")
frame = Frame(root)
frame.pack()
Var1 = StringVar()
RBttn = Radiobutton(frame, text = "Option1", variable = Var1,
value = 1)
RBttn.pack(padx = 5, pady = 5)
RBttn2 = Radiobutton(frame, text = "Option2", variable = Var1,
value = 2)
RBttn2.pack(padx = 5, pady = 5)
root.mainloop()
Label:
A Tkinter widget used to display simple lines of text on a GUI. Also very versatile, and can even be used to display images.
from tkinter import *
root = Tk()
root.geometry("200x150")
frame = Frame(root)
frame.pack()
var = StringVar()
var.set("Hello World")
label = Label(frame, textvariable = var )
label.pack()
root.mainloop()
Menu:
The Tkinter Menu widget is used to create various types of menus in the GUI such as top-level menus, which are displayed right under the title bar of the parent window.
This is fairly complex widget, but essential in creating a modern and powerful GUI.
from tkinter import *
def save():
#Code to be written
pass
def load():
#Code to be written
pass
root = Tk()
root.geometry("200x150")
frame = Frame(root)
frame.pack()
mainmenu = Menu(frame)
mainmenu.add_command(label = "Save", command= save)
mainmenu.add_command(label = "Load", command= load)
mainmenu.add_command(label = "Exit", command= root.destroy)
root.config(menu = mainmenu)
root.mainloop()
ComboBox:
A special extension of Tkinter, the ttk
module brings forward this widget. A combobox presents a drop down list of options and displays them one at a time. Has a more modern approach than other similar widgets.
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("200x150")
frame = Frame(root)
frame.pack()
vlist = ["Option1", "Option2", "Option3",
"Option4", "Option5"]
Combo = ttk.Combobox(frame, values = vlist)
Combo.set("Pick an Option")
Combo.pack(padx = 5, pady = 5)
root.mainloop()
List Box:
Another Tkinter widget that is used to display a list of options for the user to select. Displays all of the options in one go. Furthermore, all options are in text format.
from tkinter import *
root = Tk()
root.geometry("200x220")
frame = Frame(root)
frame.pack()
label = Label(root,text = "A list of Grocery items.")
label.pack()
listbox = Listbox(root)
listbox.insert(1,"Bread")
listbox.insert(2, "Milk")
listbox.insert(3, "Meat")
listbox.insert(4, "Cheese")
listbox.insert(5, "Vegetables")
listbox.pack()
root.mainloop()
Menu Button:
A combination of both the button and menu widget, this button widget displays a drop down menu with a list of options once clicked. This widget is unique because it’s able to incorporate aspects of both check buttons and radio buttons into it.
from tkinter import *
root = Tk()
root.geometry("200x150")
frame = Frame(root)
frame.pack()
MenuBttn = Menubutton(frame, text = "Favourite food", relief = RAISED)
Var1 = IntVar()
Var2 = IntVar()
Var3 = IntVar()
Menu1 = Menu(MenuBttn, tearoff = 0)
Menu1.add_checkbutton(label = "Pizza", variable = Var1)
Menu1.add_checkbutton(label = "Cheese Burger", variable = Var2)
Menu1.add_checkbutton(label = "Salad", variable = Var3)
MenuBttn["menu"] = Menu1
MenuBttn.pack()
root.mainloop()
Canvas:
One of the more advanced Tkinter widgets. As the name suggests, it’s used to draw graphs and plots on. You can even display images on a Canvas. It’s like a drawing board on which you can paint or draw anything.
from tkinter import *
root = Tk()
frame=Frame(root,width=300,height=300)
frame.pack(expand = True, fill=BOTH)
canvas = Canvas(frame,bg='white', width = 300,height = 300)
coordinates = 20, 50, 210, 230
arc = canvas.create_arc(coordinates, start=0, extent=250, fill="blue")
arc = canvas.create_arc(coordinates, start=250, extent=50, fill="red")
arc = canvas.create_arc(coordinates, start=300, extent=60, fill="yellow")
canvas.pack(expand = True, fill = BOTH)
root.mainloop()
Scale:
The Tkinter Scale widget is used to implement a graphical slider to the User interface giving the user the option of picking through a range of values. The Maximum and minimum values on the Scale can be set the programmer.
from tkinter import *
root = Tk()
root.geometry("200x200")
frame = Frame(root)
frame.pack()
Scala = Scale(frame, from_=0, to=10)
Scala.pack(padx=5, pady=5)
Scala2 = Scale(frame, from_=0, to=10, orient=HORIZONTAL)
Scala2.pack(padx=5, pady=5)
root.mainloop()
Scrollbar:
A useful widget in GUI’s, which allows you to scroll in a Tkinter window or enable scroll for certain widgets. Typically used when you’re limited in space for your Tkinter window, but want more space for the widget (e.g Canvas).
from tkinter import *
root = Tk()
root.geometry("200x250")
mylabel = Label(root, text ='Scrollbars', font = "30")
mylabel.pack()
myscroll = Scrollbar(root)
myscroll.pack(side = RIGHT, fill = Y)
mylist = Listbox(root, yscrollcommand = myscroll.set )
for line in range(1, 100):
mylist.insert(END, "Number " + str(line))
mylist.pack(side = LEFT, fill = BOTH )
myscroll.config(command = mylist.yview)
root.mainloop()
Toplevel:
A widget in Tkinter that allows for the easy spawning of new Tkinter Windows. Toplevel is a better alternative to spawning extra tkinter windows by using tk()
.
from tkinter import *
def NewWindow():
window = Toplevel()
window.geometry('150x150')
newlabel = Label(window, text = "Settings Window")
newlabel.pack()
root = Tk()
root.geometry('200x200')
myframe = Frame(root)
myframe.pack()
mybutton = Button(myframe, text = "Settings", command = NewWindow)
mybutton.pack(pady = 10)
root.mainloop()
Further Reading
There’s a lot more to Tkinter than just the above widgets. It’s important to know how to properly use these widgets along with many supporting functions to maximize their potential.
This marks the end of the Tkinter Widgets List article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.