You might have noticed that on running our Tkinter application the window or the overall look and feel is not great and appealing. It is important that an application looks nice, which will encourage users to use.
By default when you run your Tkinter application on windows it will apply theme named "Vista" on them, correspondingly theme named "MacOX" on Mac and theme named "Ubuntu" on Ubuntu. Apart from these native theme we can download multiple in-build or third party themes to our windows and widgets in our Tkinter application.
In this and upcoming post we will see how to apply themes on our Tkinker application. Lets get started,
IN[1]
from tkinter import *
from tkinter import ttk
from functools import partial
# Enabling High DPI Awareness
try :
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
except :
pass
def raise_frame(frame):
frame.tkraise()
root = Tk()
root.geometry('550x500')
root.title("Registration Form")
second_frame = Frame(root)
second_frame.place(x=0, y=0, width=500, height=500)
first_frame = Frame(root)
first_frame.place(x=0, y=0, width=500, height=500)
label_0 = Label(first_frame, text="Registration form",width=20,font=("bold", 20))
label_0.place(x=90,y=53)
label_1 = Label(first_frame, text="FullName",width=20,font=("bold", 10))
label_1.place(x=80,y=130)
entry_1 = Entry(first_frame)
entry_1.place(x=240,y=130)
label_2 = Label(first_frame, text="Email",width=20,font=("bold", 10))
label_2.place(x=68,y=180)
entry_2 = Entry(first_frame)
entry_2.place(x=240,y=180)
label_3 = Label(first_frame, text="Gender",width=20,font=("bold", 10))
label_3.place(x=70,y=230)
var = IntVar()
Radiobutton(first_frame, text="Male",padx = 5, variable=var, value=1).place(x=235,y=230)
Radiobutton(first_frame, text="Female",padx = 20, variable=var, value=2).place(x=290,y=230)
label_4 = Label(first_frame, text="country",width=20,font=("bold", 10))
label_4.place(x=70,y=280)
list1 = ['Canada','India','UK','Nepal','Iceland','South Africa'];
c=StringVar()
droplist=OptionMenu(first_frame,c, *list1)
droplist.config(width=15)
c.set('select your country')
droplist.place(x=240,y=280)
label_4 = Label(first_frame, text="Programming",width=20,font=("bold", 10))
label_4.place(x=85,y=330)
var1 = IntVar()
Checkbutton(first_frame, text="java", variable=var1).place(x=235,y=330)
var2 = IntVar()
Checkbutton(first_frame, text="python", variable=var2).place(x=290,y=330)
Button(first_frame, text='Submit',width=20,bg='brown',fg='white', command=lambda:raise_frame(second_frame)).place(x=180,y=380)
label_8 = Label(second_frame, text="Welcome to page 2",width=20,font=("bold", 10))
label_8.place(x=70,y=230)
Button(second_frame, text="Switch back to page 1",width=20,bg='brown',fg='white', command=lambda:raise_frame(first_frame)).place(x=180,y=380)
root.mainloop()
OUT[1]
Above is the source code from internet, which is a Tkinter Registration Form application and this is how it looks as it runs on windows. Now we gonna define a style database and check what is the current theme used.
IN[2]
# Defining a style database
style = ttk.Style(root)
# Getting current theme name
print(style.theme_use())
OUT[2]
By default, this Style database comes with a list of themes, by using the method style.theme_names() we can get a list of themes.
IN[3]
style = ttk.Style(root)
print(style.theme_names())
OUT[3]
From above method will can find out list of available themes so now we can apply themes from anyone of them by using the method theme_use as shown below,
# Setting the current theme
style.theme_use("winnative")
In Tkinter Styles are not just an aesthetic part and these style can alter or change the way the widgets interact when applied on them.