Trusted answers to developer questions

What are the basic functions of Tkinter in Python?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

Tkinter is a GUI library in Python that is a fast and easy way to create GUI applications.

Initialization

To open Tkinter, follow the following steps:

  1. Import Tkinter module
  2. Initialize an object to it and add required widgets
  3. Add object name.mainloop()

The following steps are shown in the code below:

from tkinter import *
obj = Tk()
obj.mainloop()

Buttons

The Button object is used to display the button on your application:

new_button = Button(text= "button", command ="")
new_button.pack()

Entry

The Entry object is used to display a single input text field line:

text_field = Entry()
text_field.pack()

Label

The Label object is used to display a single text field line:

main_title = Label(text = "Hello")
main_title.pack()

Geometry management

All the widget has to use is a specific geometry method and it is divided into three types. These types are:

  1. Pack: organizes the widgets before placing them in the parent widget.
new_button = Button(text= "button", command ="")
new_button.pack(side = BOTTOM)
text_field = Entry()
text_field.pack(side = LEFT)
main_title = Label(text = "Hello")
main_title.pack(side= RIGHT)
  1. Place: you can provide an absolute position for where to place the widget.
new_button = Button(text= "button", command ="")
new_button.place(height = 100, width = 100)
  1. Grid: displays widgets in Table format.
new_button = Button(text= "button",width = 40, command ="")
new_button.grid(row = 1, column = 0,columnspan = 2)
text_field = Entry()
text_field.grid(row = 0, column =1)
main_title = Label(text = "Hello")
main_title.grid(row = 0, column = 0)

RELATED TAGS

python
Did you find this helpful?