tkinter.ttk is a module that is used to style the tkinter widgets. Just like CSS is used to style an HTML element, we use tkinter.ttk to style tkinter widgets.
Here are the major differences between tkinter widget and tkinter.ttk −
Tkinter widgets are used to add Buttons, Labels, Text, ScrollBar, etc., however, tkinter.ttk supports a variety of widgets as compared to tkinter widgets.
Tkinter.ttk doesn't support Place, Pack() and Grid(), thus it is recommended to use tkinter widget with ttk.
Ttk has many features and configurations that extend the functionality of a native application and make it look more modern.
Tkinter widget is a native widget in the tkinter library, however ttk is a themed module.
To override the basic Tk widget in tkinter, use "from tkinter.ttk import *"
In the following example, we have styled a tkinter native widget using tkinter.ttk module. We will create a button that will change the background color of the text widget.
#Import the tkinter library from tkinter import * from tkinter.ttk import * #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("620x400") #Add a class to style the tkinter widgets style = ttk.Style() style.configure('TEntry', foreground = 'red') #Define a function to change the text color def change_color(): text.configure(background="red") #Create a text widget text=Label(win,text="This is a New Text",foreground="white", background="blue",font=('Aerial bold',20)) text.pack(pady=20) #Create a Button widget Button(win, text= "Click Here", command= change_color).pack(pady=10) win.mainloop()
Running the above code will produce the following output −
Now, click the "Click Here" button. It will change the background color of the text widget to Red.