Why do we use import * and then ttk in TKinter?


In order to work with a tkinter application, we have to install and import the tkinter library in our environment. Generally, we import the tkinter library in the environment by using from tkinter import * command.

The significance of "import *" represents all the functions and built-in modules in the tkinter library. By importing all the functions and methods, we can use the inbuilt functions or methods in a particular application without importing them implicitly.

There are lots of widgets, functions, methods available in tkinter library which can be used to construct the component of a particular application. Tkinter provides the ttk package that is used to style the widget's property and its look and feel. In order to use the ttk package, we have to import it by typing the following code −;

from tkinter import ttk

Example

In this particular example, we will create a functional application that will contain a button and a label widget.

#Import tkinter library
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter frame or window
win= Tk()
#Set the geometry of tkinter frame
win.geometry("750x250")
#Define the function to close the window
def change_text():
   label.configure(text="Welcome")
#Create a label
label=Label(win, text= "Click the below button to Change this Text", font=('Aerial 20 bold'))
label.pack(pady=30)
#Create a button widget
button= ttk.Button(win, text="Commit",command=lambda:change_text())
button.pack()
win.mainloop()

Output

Executing the above code will display a window that contains a button and a text label showing some text. When we click the button, it will change the message on the screen.

Now, click the "Commit" button to change the Label text.

Updated on: 22-Apr-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements