Light or Dark Theme Changer using Tkinter
Last Updated :
11 Jul, 2022
In this article, we are going to create a GUI application for Light/Dark Theme Changer using Tkinter in Python.
Have you thought of creating a Theme Changer using Python? In this article, we are going to learn how to create a simple Theme Changer in Python using Tkinter.
Python provides a number of libraries to address our needs and Tkinter is one of them. Tkinter is a standard library in Python which can be used to create Graphical User Interface applications easily. It provides a powerful toolkit with the help of which we can create pages or windows and add widgets like buttons, labels, scrollbars, and text boxes to them.
Prerequisites
- We must have basic knowledge of Python.
- Tkinter module should be installed on the system.
- Light and dark switch images in .png format which can be downloaded from Google.
- We should have basic knowledge of cropping images.
Installation
Tkinter can be installed by running the following command in the command prompt:
pip install tk
Stepwise Implementation
Now, we will write the Python script for our Theme Changer.
Step 1: Creating a simple Tkinter window.
Consider the code below:
Python3
from tkinter import *
window = Tk()
window.title( "Theme Changer" )
window.geometry( "600x800" )
window.config(bg = "white" )
window.mainloop()
|
Output:
Step 2: Now, we will add the Light/Dark Mode Button on the window.
In Tkinter, we can use images as buttons. To do this we need to create a PhotoImage object and pass the image argument to it. The following syntax can be used to create a PhotoImage object:
PhotoImage(file=path_of_the_image)
- Button widget is used to create a button in the window. Here, a button with light.png and white background color will be created.
- Pack() method is used to set the position of the button on the window.
Python3
from tkinter import *
window = Tk()
window.title( "Theme Changer" )
window.geometry( "600x800" )
window.config(bg = "white" )
light = PhotoImage( file = "light.png" )
dark = PhotoImage( file = "dark.png" )
switch = Button(window, image = light,
bd = 0 , bg = "white" )
switch.pack(padx = 50 , pady = 150 )
window.mainloop()
|
Output:
Step 3: Now, we will create a function to switch between the themes.
Python3
from tkinter import *
window = Tk()
window.title( "Theme Changer" )
window.geometry( "600x800" )
window.config(bg = "white" )
light = PhotoImage( file = "light.png" )
dark = PhotoImage( file = "dark.png" )
switch_value = True
def toggle():
global switch_value
if switch_value = = True :
switch.config(image = dark, bg = "#26242f" ,
activebackground = "#26242f" )
window.config(bg = "#26242f" )
switch_value = False
else :
switch.config(image = light, bg = "white" ,
activebackground = "white" )
window.config(bg = "white" )
switch_value = True
switch = Button(window, image = light,
bd = 0 , bg = "white" ,
activebackground = "white" ,
command = toggle)
switch.pack(padx = 50 , pady = 150 )
window.mainloop()
|
Output:
Share your thoughts in the comments
Please Login to comment...