1 | initial version |
One way to modify a Simple Python Tkinter program to include an on/off function for Dark-mode is by implementing the following steps:
First, create a variable to store the state of the Dark-mode feature. For example, we can create a boolean variable called dark_mode_enabled
and initialize it to False
by default.
# Initialize the Dark mode state variable
dark_mode_enabled = False
Next, define a function that toggles the state of the dark_mode_enabled
variable. This function will be called whenever the user clicks on the Dark-mode button.
def toggle_dark_mode():
global dark_mode_enabled
dark_mode_enabled = not dark_mode_enabled
# Update the color scheme of the GUI components based on the Dark-mode state
if dark_mode_enabled:
# Set the background and foreground colors to a darker shade
root.config(bg="#222222")
label.config(bg="#222222", fg="#FFFFFF")
button.config(bg="#FFFFFF", fg="#222222")
else:
# Set the background and foreground colors to the default shade
root.config(bg="#FFFFFF")
label.config(bg="#FFFFFF", fg="#222222")
button.config(bg="#222222", fg="#FFFFFF")
Note that in this function, we are using the global
keyword to access the dark_mode_enabled
variable, which is defined outside this function.
Also, we are updating the color scheme of the GUI components based on the state of the dark_mode_enabled
variable. When the Dark-mode is enabled, we set the background and foreground colors to a darker shade, and when it is disabled, we set them to the default shade.
Finally, we need to create a button that calls the toggle_dark_mode
function when clicked. We can use a tkinter.Button
widget and set its command
option to the toggle_dark_mode
function.
# Create a button to toggle the Dark-mode feature
button = tkinter.Button(root, text="Dark-mode", command=toggle_dark_mode)
button.pack()
Here's the complete program with the Dark-mode feature:
import tkinter
# Initialize the Tkinter window
root = tkinter.Tk()
# Set the window title
root.title("Simple Tkinter Program")
# Set the window size
root.geometry("400x200")
# Initialize the Dark mode state variable
dark_mode_enabled = False
# Define the function to toggle the Dark-mode feature
def toggle_dark_mode():
global dark_mode_enabled
dark_mode_enabled = not dark_mode_enabled
# Update the color scheme of the GUI components based on the Dark-mode state
if dark_mode_enabled:
# Set the background and foreground colors to a darker shade
root.config(bg="#222222")
label.config(bg="#222222", fg="#FFFFFF")
button.config(bg="#FFFFFF", fg="#222222")
else:
# Set the background and foreground colors to the default shade
root.config(bg="#FFFFFF")
label.config(bg="#FFFFFF", fg="#222222")
button.config(bg="#222222", fg="#FFFFFF")
# Create a label widget
label = tkinter.Label(root, text="Hello, World!")
label.pack()
# Create a button to toggle the Dark-mode feature
button = tkinter.Button(root, text="Dark-mode", command=toggle_dark_mode)
button.pack()
# Start the Tkinter event loop
root.mainloop()
In this program, we have added a Dark-mode button that toggles the state of the Dark-mode feature. When the Dark-mode is enabled, the background and foreground colors of the GUI components are set to a darker shade, and when it is disabled, they are set to the default shade.