etutorialspoint
  • Home
  • PHP
  • MySQL
  • MongoDB
  • HTML
  • Javascript
  • Node.js
  • Express.js
  • Python
  • Jquery
  • R
  • Kotlin
  • DS
  • Blogs
  • Theory of Computation

Python Tkinter Tutorial with Examples

Tkinter is a standard cross-platform package for creating graphical user interfaces (GUIs). It is also called 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, checkbox, scale, 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-

Python Tkinter




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-

Python Tkinter



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-

Python Tkinter Button



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-

Python Tkinter Radio Button

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-

Python Tkinter Checkbox



Related Articles

Python Tkinter Combobox Event Binding
Python 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




Most Popular Development Resources
Retrieve Data From Database Without Page refresh Using AJAX, PHP and Javascript
-----------------
PHP Create Word Document from HTML
-----------------
How to get data from XML file in PHP
-----------------
Hypertext Transfer Protocol Overview
-----------------
PHP code to send email using SMTP
-----------------
Characteristics of a Good Computer Program
-----------------
How to encrypt password in PHP
-----------------
Create Dynamic Pie Chart using Google API, PHP and MySQL
-----------------
PHP MySQL PDO Database Connection and CRUD Operations
-----------------
Splitting MySQL Results Into Two Columns Using PHP
-----------------
Dynamically Add/Delete HTML Table Rows Using Javascript
-----------------
How to get current directory, filename and code line number in PHP
-----------------
How to add multiple custom markers on google map
-----------------
Get current visitor\'s location using HTML5 Geolocation API and PHP
-----------------
Fibonacci Series Program in PHP
-----------------
Simple star rating system using PHP, jQuery and Ajax
-----------------
How to Sort Table Data in PHP and MySQL
-----------------
Simple pagination in PHP with MySQL
-----------------
How to generate QR Code in PHP
-----------------
Submit a form data using PHP, AJAX and Javascript
-----------------
PHP MYSQL Advanced Search Feature
-----------------
jQuery loop over JSON result after AJAX Success
-----------------
Recover forgot password using PHP7 and MySQLi
-----------------
PHP Server Side Form Validation
-----------------
jQuery File upload progress bar with file size validation
-----------------
PHP user registration and login/ logout with secure password encryption
-----------------
To check whether a year is a leap year or not in php
-----------------
Php file based authentication
-----------------
Simple File Upload Script in PHP
-----------------
Simple PHP File Cache
-----------------
PHP User Authentication by IP Address
-----------------
Calculate the distance between two locations using PHP
-----------------
PHP Secure User Registration with Login/logout
-----------------
Polling system using PHP, Ajax and MySql
-----------------
How to print specific part of a web page in javascript
-----------------
Detect Mobile Devices in PHP
-----------------
Simple Show Hide Menu Navigation
-----------------
Simple way to send SMTP mail using Node.js
-----------------
SQL Injection Prevention Techniques
-----------------
Get Visitor\'s location and TimeZone
-----------------
Preventing Cross Site Request Forgeries(CSRF) in PHP
-----------------
Google Street View API Example
-----------------
PHP Sending HTML form data to an Email
-----------------
Driving route directions from source to destination using HTML5 and Javascript
-----------------
CSS Simple Menu Navigation Bar
-----------------
Date Timestamp Formats in PHP
-----------------
PHP Programming Error Types
-----------------
Convert MySQL to JSON using PHP
-----------------
Set and Get Cookies in PHP
-----------------
How to add google map on your website and display address on click marker
-----------------
How to select/deselect all checkboxes using Javascript
-----------------
PHP Getting Document of Remote Address
-----------------
How to display PDF file in web page from Database in PHP
-----------------
File Upload Validation in PHP
-----------------
PHP FTP Connection and File Handling
-----------------


Most Popular Blogs
Most in demand programming languages
Best mvc PHP frameworks in 2019
MariaDB vs MySQL
Most in demand NoSQL databases for 2019
Best AI Startups In India
Kotlin : Android App Development Choice
Kotlin vs Java which one is better
Top Android App Development Languages in 2019
Web Robots
Data Science Recruitment of Freshers - 2019


Interview Questions Answers
Basic PHP Interview
Advanced PHP Interview
MySQL Interview
Javascript Interview
HTML Interview
CSS Interview
Programming C Interview
Programming C++ Interview
Java Interview
Computer Networking Interview
NodeJS Interview
ExpressJS Interview
R Interview


Popular Tutorials
PHP Tutorial (Basic & Advance)
MySQL Tutorial & Exercise
MongoDB Tutorial
Python Tutorial & Exercise
Kotlin Tutorial & Exercise
R Programming Tutorial
HTML Tutorial
jQuery Tutorial
NodeJS Tutorial
ExpressJS Tutorial
Theory of Computation Tutorial
Data Structure Tutorial
Javascript Tutorial




General Knowledge

listen
listen
listen
listen
listen
listen
listen
listen
listen


Learn Popular Language

listen
listen
listen
listen
listen

Blogs

  • Jan 3

    Stateful vs Stateless

    A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

  • Dec 29

    Best programming language to learn in 2021

    In this article, we have mentioned the analyzed results of the best programming language for 2021...

  • Dec 20

    How is Python best for mobile app development?

    Python has a set of useful Libraries and Packages that minimize the use of code...

  • July 18

    Learn all about Emoji

    In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

  • Jan 10

    Data Science Recruitment of Freshers

    In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

Follow us

  • etutorialspoint facebook
  • etutorialspoint twitter
  • etutorialspoint linkedin
etutorialspoint youtube
About Us      Contact Us


  • eTutorialsPoint©Copyright 2016-2022. All Rights Reserved.