EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Tkinter Tutorial Python Tkinter
Secondary Sidebar
Tkinter Tutorial
  • Tkinter
    • Python Tkinter
    • Tkinter Widgets
    • Tkinter background image
    • Tkinter button color
    • Tkinter place
    • Python Tkinter Button
    • Python Tkinter Canvas
    • Tkinter Frame
    • Tkinter LabelFrame
    • Python Tkinter Label
    • Tkinter Text
    • Tkinter Scrollbar
    • Tkinter Listbox
    • Tkinter Spinbox
    • Tkinter Checkbutton
    • Tkinter Menu
    • Tkinter Menubutton
    • Tkinter OptionMenu
    • Tkinter Messagebox
    • Tkinter Grid
    • Tkinter Pack
    • Tkinter Scale
    • Tkinter Table
    • Python Tkinter Entry
    • Tkinter after
    • Tkinter Colors
    • Tkinter Font
    • Tkinter PhotoImage
    • Tkinter TreeView
    • Tkinter Notebook
    • Tkinter Combobox
    • Tkinter Bind
    • Tkinter Icon
    • Tkinter Window Size
    • Tkinter Color Chart
    • Tkinter Slider
    • Tkinter Calculator
    • Tkinter geometry
    • Tkinter image
    • Tkinter input box
    • Tkinter mainloop

Python Tkinter

By Priya PedamkarPriya Pedamkar

Python-Tkinter

Introduction to Python Tkinter

Python Tkinter is the standard Graphical User Interface (GUI) that is supported in Python. When Tkinter is used alongside Python, it results in the creation of GUI very conveniently and quite fast. The main advantage of using Tkinter is that it has an object-oriented interface. The success of any application or website depends on how well it interacts with the user, i.e. an attractive graphical interface helps to create a good application. Well, Python Tkinter does that for us.

Syntax:

The following are the steps for creating a Tkinter application along with the syntax:

  • First, we need to import the Tkinter module.
  • Secondly, we need to create a container window.
  • Then we add any number of widgets to the container window.
  • Lastly, we apply the event trigger on the widgets.

# Here, we import the Tkinter module

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

import tkinter

# Here, we create container window object

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,697 ratings)
c = tkinter.Tk()

# widgets that need to be added

# This executes the application until and unless it is stopped

c.mainloop()

Why do we use Python Tkinter?

Python Tkinter is the most preferred package used for creating nice GUIs for applications as it has a variety of methods like pack(), grid(), and place() for geometry management. It has standard attributed dimensions, fonts, colors, cursors, anchors, and bitmaps for better GUI. Moreover, it has a vast array of widgets to choose from and is by far the easiest to use. The combination of all these features makes Python Tkinter makes it very popular among Python developers and makes it a favorable tool to use.

Methods of Python Tkinter

The following are the methods used in Python Tkinter for geometry management:-

1. pack()

It places the child widget in blocks before placing it inside the parent widget.

Syntax:

widget.pack(pack_options)

The possible options for pack_options are the following:

  • expand: It is a Boolean which can either be True or False. When it is set to True, the child widget expands to fill the parent widget’s full space.
  • fill: The default value is none. The other values being X (fill only horizontally), Y (fill only vertically) and both (fill both horizontally and vertically)
  • side: It determines the side of the parent widget against which the child widget packs against. The default value is the top. The other values are bottom, left, and right.

Example:

from tkinter import *
root = Tk()
label1 = Label(root,text="This is a tutorial about Python Tkinter")
label1.pack(side=TOP,expand=True)
label2 = Label(root,text="Do you wish to learn?",fg="blue")
label2.pack(side=TOP,expand=True)
button1 = Button(root, text="Accept", fg="green",command=root.destroy)
button1.pack(side=LEFT,expand=True)
button2 = Button(root, text="Close", fg="red",command=root.destroy)
button2.pack(side=RIGHT,expand=True)
root.mainloop()

Output:

Python Tkinter 1

2. grid()

It places the child widget in grids before placing it inside the parent widget.

Syntax:

widget.grid(grid_options)

The possible options for grid_options are the following:

  • column: It determines the column to put the child widget inside the parent widget. The default value is 0, which signifies the leftmost column.
  • columnspan: It determines the number of columns the widget occupies. The default value is 1.
  • ipadx, ipady: It signifies the horizontal and vertical padding for the child widget inside the parent widget’s border in terms of pixels.
  • padx, pady: It signifies the horizontal and vertical padding for the child widget outside the parent widget’s border in terms of pixels.
  • row: It signifies the row to put the widget in.
  • rowspan: It determines the number of rows the widget occupies. The default value being 1.

Example:

from tkinter import *
root = Tk()
for r in range(9):
   for c in range(9):
       Label(root , text = 'R%s/C%s'%(r,c),
             borderwidth = 1).grid(row = r , column = c , rowspan = 1 , columnspan = 1 , padx = 3 , pady = 3)
root.mainloop()

Output:

Python Tkinter 2

3. place()

It organizes the widgets by placing them on a specific position as indicated by the developer.

Syntax:

widget.place(place_options)

The possible options for place_options are the following:

  • anchor: This determines the exact position of the widget. The values correspond to the different directions available in the compass. The possible values are N,S,E,W,NE,NW,SE and SW. The default value is NW which is at the upper left corner.
  • bordermode: The possible values are inside and outside. The default being inside.
  • height, width: This indicates the height and weight in pixels.
  • relheight, relwidth: This indicates the height and width relative to the parent widget’s height and width. This is a floating-point number between 0.0 and 1.0
  • relx, rey: This indicates the offset of the child widget relative to the parent widget. It is a floating-point number between 0.0 and 1.0
  • x, y: This indicates the vertical and horizontal offset of the child widget in pixel.

Example:

from tkinter import *
root = Tk()
button1 = Button(root, text ="Hello" , fg="blue")
button1.pack(side = TOP)
button1.place(anchor = E , relheight = 0.25 , relwidth = 0.25 , relx = 0.4 , rely = 0.5)
button2 = Button(root, text = "World" , fg = "green")
button2.pack(side = BOTTOM)
button2.place(anchor = W , relheight = 0.25 , relwidth = 0.25 , relx=0.6 , rely= 0.5)
root.mainloop()

Output:

place options

Conclusion

It is finally time to draw closure to this article on Python Tkinter. In this article, we have discussed briefly the basics of Python Tkinter. We’ve mentioned why we’re using Python Tkinter, and we’ve seen the syntax of using it. Last but not least, we saw the process used in Python Tkinter along with Examples. So next time you decide to create a user-friendly interface, remember to use Python Tkinter.

Recommended Articles

This is a guide to Python Tkinter. Here we discuss the introduction and different methods of Python Tkinter with syntax and examples. You may also have a look at the following articles to learn more –

  1. Python Features
  2. Python Reverse String
  3. Tkinter Checkbutton
  4. Tkinter Scrollbar
Popular Course in this category
Python Certifications Training Program (40 Courses, 13+ Projects)
  40 Online Courses |  13 Hands-on Projects |  215+ Hours |  Verifiable Certificate of Completion
4.8
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more