Python Tkinter - GeeksforGeeks
Python Tkinter - GeeksforGeeks
Python Tkinter
Last Updated : 19 Jun, 2024
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI
methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI
toolkit shipped with Python. Python Tkinter is the fastest and easiest way to create GUI applications.
Creating a GUI using Tkinter is an easy task.
Table of Content
Create First Tkinter GUI Application
Tkinter Widget
Color and Font Option in Tkinter
Geometry Management
1. Import the tkinter module: This is done just like importing any other module in Python. Note that
in Python 2.x, the module is named ‘Tkinter’, while in Python 3.x, it is named ‘tkinter’.
2. Create the main window (container): The main window serves as the container for all the GUI
elements you’ll add later.
3. Add widgets to the main window: You can add any number of widgets like buttons, labels, entry
fields, etc., to the main window to design the interface as desired.
4. Apply event triggers to the widgets: You can attach event triggers to the widgets to define how
they respond to user interactions.
Tk()
mainloop()
There is a method known by the name mainloop() is used when your application is ready to run.
mainloop() is an infinite loop used to run the application, wait for an event to occur, and process the
event as long as the window is not closed.
Example
Python
import tkinter
m = tkinter.Tk()
'''
widgets are added here
'''
m.mainloop()
Output
Tkinter Widget
There are a number of widgets which you can put in your tkinter application. Some of the major
widgets are explained below:
1. 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.
Python
Output
2. 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.
Python
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()
Output
3. 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.
Python
master = Tk()
Label(master, text='First Name').grid(row=0)
Label(master, text='Last Name').grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
mainloop()
Output
4. 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.
Python
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()
Output
5. RadioButton
It is used to offer multi-choice option to the user. It offers several options to the user and the user has
to choose one option. The general syntax is:
w = RadioButton(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.
Python
root = Tk()
v = IntVar()
Radiobutton(root, text='GfG', variable=v, value=1).pack(anchor=W)
Radiobutton(root, text='MIT', variable=v, value=2).pack(anchor=W)
mainloop()
Output
6. 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.
Python
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()
Output
7. Scrollbar
It refers to the slide controller which will be used to implement listed widgets. The general syntax is:
w = Scrollbar(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.
Python
root = Tk()
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
mylist = Listbox(root, yscrollcommand=scrollbar.set)
mylist.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=mylist.yview)
mainloop()
Output
8. Menu
It is used to create all kinds of menus used by the application. The general syntax is:
w = Menu(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 this widget. Number of options
can be passed as parameters separated by commas. Some of them are listed below.
Python
root = Tk()
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label='File', menu=filemenu)
filemenu.add_command(label='New')
filemenu.add_command(label='Open...')
filemenu.add_separator()
filemenu.add_command(label='Exit', command=root.quit)
helpmenu = Menu(menu)
menu.add_cascade(label='Help', menu=helpmenu)
helpmenu.add_command(label='About')
mainloop()
Output
9. Combobox
Combobox widget is created using the ttk.Combobox class from the tkinter.ttk module. The values for
the Combobox are specified using the values parameter. The default value is set using the set
method. An event handler function on_select is bound to the Combobox using the bind method,
which updates a label with the selected item whenever an item is selected.
Python
import tkinter as tk
from tkinter import ttk
def on_select(event):
selected_item = combo_box.get()
label.config(text="Selected Item: " + selected_item)
root = tk.Tk()
root.title("Combobox Example")
# Create a label
label = tk.Label(root, text="Selected Item: ")
label.pack(pady=10)
root.mainloop()
Output
10. Scale
It is used to provide a graphical slider that allows to select any value from that scale. The general
syntax is:
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.
Python
master = Tk()
w = Scale(master, from_=0, to=42)
w.pack()
w = Scale(master, from_=0, to=200, orient=HORIZONTAL)
w.pack()
mainloop()
Output
11. TopLevel
This widget is directly controlled by the window manager. It don’t need any parent window to work
on.The general syntax is:
w = TopLevel(master, option=value)
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.
Python
root = Tk()
root.title('GfG')
top = Toplevel()
top.title('Python')
top.mainloop()
Output
12. Message
It refers to the multi-line and non-editable text. It works same as that of Label. The general syntax is:
w = Message(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.
Python
main = Tk()
ourMessage = 'This is our Message'
messageVar = Message(main, text=ourMessage)
messageVar.config(bg='lightgreen')
messageVar.pack()
main.mainloop()
Output
13. MenuButton
It is a part of top-down menu which stays on the window all the time. Every menubutton has its own
functionality. The general syntax is:
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.
Python
top = Tk()
mb = Menubutton ( top, text = "GfG")
mb.grid()
mb.menu = Menu ( mb, tearoff = 0 )
mb["menu"] = mb.menu
cVar = IntVar()
aVar = IntVar()
mb.menu.add_checkbutton ( label ='Contact', variable = cVar )
mb.menu.add_checkbutton ( label = 'About', variable = aVar )
mb.pack()
top.mainloop()
Output
14. Progressbar
Tkinter application with a Progressbar widget and a button to start the progress. When the button is
clicked, the progressbar fills up to 100% over a short period, simulating a task that takes time to
complete.
Python
import tkinter as tk
from tkinter import ttk
import time
def start_progress():
progress.start()
root = tk.Tk()
root.title("Progressbar Example")
root.mainloop()
Output
15. SpinBox
It is an entry of ‘Entry’ widget. Here, value can be input by selecting a fixed value of numbers.The
general syntax is:
w = SpinBox(master, option=value)
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.
Python
master = Tk()
w = Spinbox(master, from_=0, to=10)
w.pack()
mainloop()
Output:
16. Text
To edit a multi-line text and format the way it has to be displayed. The general syntax is:
w =Text(master, option=value)
There are number of options which are used to change the format of the text. Number of options can
be passed as parameters separated by commas. Some of them are listed below.
Python
root = Tk()
T = Text(root, height=2, width=30)
T.pack()
T.insert(END, 'GeeksforGeeks\nBEST WEBSITE\n')
mainloop()
Output
17. 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.
Python
master = Tk()
w = Canvas(master, width=40, height=60)
w.pack()
canvas_height=20
canvas_width=200
y = int(canvas_height / 2)
w.create_line(0, y, canvas_width, y )
mainloop()
Outpu
18. PannedWindow
It is a container widget which is used to handle number of panes arranged in it. The general syntax is:
w = PannedWindow(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.
Python
m1 = PanedWindow()
m1.pack(fill=BOTH, expand=1)
left = Entry(m1, bd=5)
m1.add(left)
m2 = PanedWindow(m1, orient=VERTICAL)
m1.add(m2)
top = Scale(m2, orient=HORIZONTAL)
m2.add(top)
mainloop()
Output
Python
import tkinter as tk
root = tk.Tk()
root.title("Color Options in Tkinter")
root.mainloop()
Output
Geometry Management
Tkinter also offers access to the geometric configuration of the widgets which can organize the
widgets in the parent windows. There are mainly three geometry manager classes class.
pack() method
Python
import tkinter as tk
root = tk.Tk()
root.title("Pack Example")
Output
grid() method
It organizes the widgets in grid (table-like structure) before placing in the parent widget.
Python
import tkinter as tk
root = tk.Tk()
root.title("Grid Example")
root.mainloop()
Output
place() method
It organizes the widgets by placing them on specific positions directed by the programmer.
Python
import tkinter as tk
root = tk.Tk()
root.title("Place Example")
# Create a label
label = tk.Label(root, text="Label")
root.mainloop()
Output
Tkinter is a built-in library in Python for creating graphical user interfaces (GUIs). You can use it
to design desktop applications with familiar elements like buttons, windows, menus, and more.
It allows you to build interactive programs that users can navigate visually.
In Python, Tk() is a function call from the tkinter module, which is the standard interface to the
Tk GUI toolkit. Calling Tk() initializes a new Tkinter application, creating the main window
where all the GUI components (widgets) will be placed.
No, Tkinter is not the only GUI library for Python. There are other options like PyQt, Kivy,
wxPython, and GTK+.
There’s no single “best” GUI. The best choice depends on your needs and preferences.
However, here’s a list of GUIs in Python:
Tkinter
PyQt
wxPython
Kivy
PySide
Dear PyGui
Looking to dive into the world of programming or sharpen your Python skills? Our Master Python:
Complete Beginner to Advanced Course is your ultimate guide to becoming proficient in Python.
This course covers everything you need to build a solid foundation from fundamental programming
concepts to advanced techniques. With hands-on projects, real-world examples, and expert
guidance, you'll gain the confidence to tackle complex coding challenges. Whether you're starting
from scratch or aiming to enhance your skills, this course is the perfect fit. Enroll now and master
Python, the language of the future!
Similar Reads
Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications.
Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the…
4 min read