Python Tkinter Tutorial with Examples
In this post, you will learn about the Python tkinter with examples.
Tkinter is a standard cross-platform package for creating graphical user interfaces (GUIs). It is also called the Tk interface. It is an original GUI library for Tcl (Tool Command Language). Tkinter comes pre-installed with Python. The greatest strength of Tkinter is its simplicity. It comes with a wide range of necessary widgets for almost all common tasks, like text, buttons, radio buttons, checkboxes, scales, labels, or drawing canvas.
Importing Tkinter in Python 3.x
import tkinter as tk
from tkinter import filedialog
If you want to check the presence of Tkinter in your machine, you can check this with the following command in your python REPL.
import tkinter as tk
tk._test()
First Tkinter Program
In the above code, we have imported the tkinter module. Now, we will create a top level object using the Tk class. This creates a window for your entire GUI application. This windowing object can contain different widgets, like- text labels, buttons, radio buttons, etc.
obj = tk.Tk()
The object returned by tk.Tk() is called the root window. Here is the first program to add text to the created window.
import tkinter as tk
w = tk.Tk()
# Create a text label
label = tk.Label(w, text="Hello World!")
# Pack it into the window
label.pack(padx=30, pady=30)
w.mainloop()
Output of the above code-
These are the most commonly used Tkinter widgets -
Tkinter Label widget
A label is used to display a text or image on the screen.
label(master=None, options)
Here, master is the parent widget. These options can be used as key-value pairs and separated by a comma.
Option |
Description |
bg | To display a normal background color. |
bd | To set the size of the border. |
fg | It specifies color of the text. |
font | To set the text font. |
height | To set the height of the new frame. |
image | To add static image in the label widget. |
justify | To justify multiple lines of text. |
padx | To add left and right padding of the text within the widget. |
pady | To add top and bottom padding of the text within the widget. |
text | To display one or more lines of text within widget. |
width | To set the width of the new frame. |
Label Widget Example
import tkinter as tk
w = tk.Tk()
# Create a text label
label = tk.Label(w, text="Hello World!", bg="#F4D6BC", fg="blue", font=("Helvetica", 16))
# Pack it into the window
label.pack(padx=30, pady=30)
w.mainloop()
In the above code, pack() is the Tkinter geometry manager and used to position widgets.
Output of the above code-
Tkinter Button Widget
The button widget is used to add various types of buttons in Python applications. The button can contain text or image. We can attach a python method to a button. When the button is pressed, Tkinter automatically calls that method.
b = Button(parent, options)
It also has so many options, like- activeforeground, activebackground, bd, bg, fg, font, height, width, padx, pady, relief, justify, image.
Button Widget Example
from tkinter import *
w = Tk()
w.geometry("200x100")
# Create a button
button1 = Button(w, text = "Button", width="10", bg="yellow",
activeforeground = "blue" ,activebackground = "pink",
pady=10)
# Pack it into the window
button1.pack(side = TOP)
w.mainloop()
Output of the above code-
Tkinter Radiobutton Widget
Radio button allows a user to choose one of the predefined sets of options. It can contain text or an image. We can attach a python method to the radio button. When the radio button is pressed. Tkinter automatically calls that method.
rb = Radiobutton(parent, options)
Radiobutton Widget Example
from tkinter import *
def option():
selection = "You selected the option " + str(var.get())
label.config(text = selection)
w = Tk()
var = IntVar()
Rb1 = Radiobutton(w, text="Option 1", variable=var, value=1, command=option)
Rb1.pack()
Rb2 = Radiobutton(w, text="Option 2", variable=var, value=2, command=option)
Rb2.pack()
Rb3 = Radiobutton(w, text="Option 3", variable=var, value=3, command=option)
Rb3.pack()
label = Label(w)
label.pack()
w.mainloop()
Output of the above code-
Tkinter Checkbox Widget
Checkbox allows user to make a binary choice.
rb = Checkbox(parent, options)
Checkbox Widget Example
from tkinter import *
w = Tk()
var1 = IntVar()
Checkbutton(w, text="Male", variable=var1).grid(row=0, sticky=W)
var2 = IntVar()
Checkbutton(w, text="Female", variable=var2).grid(row=1, sticky=W)
mainloop()
Output of the above code-
Related Articles
Python Tkinter Combobox Event BindingPython Tkinter Combobox
Add background image in Python Tkinter
Python Tkinter Text Widget
Countdown clock and timer using Tkinter in Python
Python Tkinter Frame Widget
Python Tkinter Checkbutton Widget
Entry Field Validation in Tkinter Python
Python Tkinter Tutorial with Examples
Python Tkinter Scale Widget
Python3 Tkinter Messagebox
Python 3 Tkinter Menu Bar
Python Tkinter Geometry Managers
Python get visitor information by IP address
Python OpenCV ColorMap
Python gmplot to add google map on a web page
Simple Calculator Program in Python
Scientific calculator in Python