Open In App

Light or Dark Theme Changer using Tkinter

Last Updated : 11 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Importing Tkinter library in 
# the environment
from tkinter import *
  
# Creating a window
window = Tk()
  
# Adding title to the window
window.title("Theme Changer")  
  
# Setting geometry of the window
window.geometry("600x800")  
  
# Setting background color of
# the window
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




# Importing Tkinter library 
# in the environment
from tkinter import *
  
# Creating a window
window = Tk()
window.title("Theme Changer")  
window.geometry("600x800"
window.config(bg="white"
  
# Adding light and dark mode images
light = PhotoImage(file="light.png")
dark = PhotoImage(file="dark.png")
  
# Creating a button to toggle
# between light and dark themes
switch = Button(window, image=light,
                bd=0, bg="white")
  
# setting the position of the button
switch.pack(padx=50, pady=150)  
  
window.mainloop()


Output:

 

Step 3: Now, we will create a function to switch between the themes. 

Python3




# Importing Tkinter library 
# in the environment
from tkinter import *
  
# Creating a window
window = Tk()
window.title("Theme Changer")  
window.geometry("600x800")
window.config(bg="white")
  
  
# Adding light and dark mode images
light = PhotoImage(file="light.png")
dark = PhotoImage(file="dark.png")
  
switch_value = True
  
# Defining a function to toggle
# between light and dark theme
def toggle():
  
    global switch_value
    if switch_value == True:
        switch.config(image=dark, bg="#26242f",
                      activebackground="#26242f")
          
        # Changes the window to dark theme
        window.config(bg="#26242f")  
        switch_value = False
  
    else:
        switch.config(image=light, bg="white"
                      activebackground="white")
          
        # Changes the window to light theme
        window.config(bg="white")  
        switch_value = True
  
  
# Creating a button to toggle
# between light and dark themes
switch = Button(window, image=light, 
                bd=0, bg="white",
                activebackground="white"
                command=toggle)
switch.pack(padx=50, pady=150)
  
window.mainloop()


Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads