Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I am doing dark mode in my python program. In this case, the color of the numbers and letters is black by default. But when I change to dark mode, in the price section that updates every 5 seconds it should be white, since the background is black, at first the price is white, but when it updates every 5 seconds, it becomes black, and I don't know what the error is.


What I have tried:

Python
is_dark_mode = False

def toggle_dark_mode():
    global is_dark_mode

    if is_dark_mode:
        root.configure(background="white")
        price_label.config(bg="white")
        text_label.config(bg="white")
        min_label.config(bg="white")
        min_entry.config(fg="black", bg="#F2F2F2")
        max_label.config(bg="white")
        max_entry.config(fg="black", bg="#F2F2F2")
        button_container.config(bg="white")
        confirm_button.config(style="TButton")
        reset_button.config(style="TButton")
        is_dark_mode = False
    else:
        root.configure(background="#222222")
        price_label.config(bg="#222222", fg="white")
        text_label.config(bg="#222222", fg="white")
        min_label.config(bg="#222222", fg="white")
        min_entry.config(fg="white", bg="#444444")
        max_label.config(bg="#222222", fg="white")
        max_entry.config(fg="white", bg="#444444")
        button_container.config(bg="#222222")
        confirm_button.config(style="TButton", fg="white", bg="#444444")
        reset_button.config(style="TButton", fg="white", bg="#444444")
        is_dark_mode = True


def toggle_normal_mode():
    global is_dark_mode

    if is_dark_mode:
        root.configure(background="#222222")
        price_label.config(bg="#222222", fg="white")
        text_label.config(bg="#222222", fg="white")
        min_label.config(bg="#222222", fg="white")
        min_entry.config(fg="white", bg="#444444")
        max_label.config(bg="#222222", fg="white")
        max_entry.config(fg="white", bg="#444444")
        button_container.config(bg="#222222")
        confirm_button.config(style="Round.TButton", fg="white", bg="#444444")
        reset_button.config(style="Round.TButton", fg="white", bg="#444444")
        is_dark_mode = True
    else:
        # Restaurar apariencia por defecto en modo normal
        root.configure(background="white")
        price_label.config(bg="white")
        text_label.config(bg="white", fg="black")
        min_label.config(bg="white", fg="black")
        min_entry.config(fg="black", bg="#F2F2F2")
        max_label.config(bg="white", fg="black")
        max_entry.config(fg="black", bg="#F2F2F2")
        button_container.config(bg="white")
        confirm_button.config(style="Round.TButton")
        reset_button.config(style="Round.TButton")
        is_dark_mode = False
Posted
Updated 3-Jul-23 6:22am
Comments
Richard MacCutchan 3-Jul-23 11:43am    
You need to show the code that calls the toggle functions. You could also simplify the above by having a single functionthat takes the colour values as parameters.

Hello !

by freezing on "toggle" word, you miss to enumerate constants for every colors, to achieve one only function :

Python
def set_color_mode(fg_val , bg_val , third_val...):

        root.configure(background=bg_val)
        price_label.config(bg=bg_val, fg=fg_val)
        text_label.config(bg=bg_val, fg=fg_val)
        min_label.config(bg=bg_val, fg=fg_val)
        min_entry.config(fg=fg_val, bg=bg_val)
        max_label.config(bg=bg=bg_val, fg=fg_val)
        #
        #
# and with one Function call :
 set_color_mode("#444444","#222222","white")
# or another style :
 set_color_mode("#515151","#123456","green")
 
Share this answer
 
v2
Comments
Jonathan Ramos Bellido 3-Jul-23 11:28am    
I did not understand, it would be of great help if you explained it to me with some details
Solution

Python
is_dark_mode = False

def toggle_dark_mode():
    global is_dark_mode

    if is_dark_mode:
        # Restaurar apariencia por defecto en modo normal
        root.configure(background="white")
        price_label.config(bg="white", fg="black")
        text_label.config(bg="white", fg="black")
        min_label.config(bg="white", fg="black")
        min_entry.config(fg="black", bg="#F2F2F2")
        max_label.config(bg="white", fg="black")
        max_entry.config(fg="black", bg="#F2F2F2")
        button_container.config(bg="white")
        confirm_button.config(style="Round.TButton")
        reset_button.config(style="Round.TButton")
        is_dark_mode = False
    else:
        root.configure(background="#222222")
        price_label.config(bg="#222222", fg="white")
        text_label.config(bg="#222222", fg="white")
        min_label.config(bg="#222222", fg="white")
        min_entry.config(fg="white", bg="#444444")
        max_label.config(bg="#222222", fg="white")
        max_entry.config(fg="white", bg="#444444")
        button_container.config(bg="#222222")
        confirm_button.config(style="TButton", fg="white", bg="#444444")
        reset_button.config(style="TButton", fg="white", bg="#444444")
        is_dark_mode = True


def toggle_normal_mode():
    global is_dark_mode

    if is_dark_mode:
        root.configure(background="#222222")
        price_label.config(bg="#222222", fg="white")
        text_label.config(bg="#222222", fg="white")
        min_label.config(bg="#222222", fg="white")
        min_entry.config(fg="white", bg="#444444")
        max_label.config(bg="#222222", fg="white")
        max_entry.config(fg="white", bg="#444444")
        button_container.config(bg="#222222")
        confirm_button.config(style="Round.TButton", fg="white", bg="#444444")
        reset_button.config(style="Round.TButton", fg="white", bg="#444444")
        is_dark_mode = True
    else:
        # Restaurar apariencia por defecto en modo normal
        root.configure(background="white")
        price_label.config(bg="white", fg="black")
        text_label.config(bg="white", fg="black")
        min_label.config(bg="white", fg="black")
        min_entry.config(fg="black", bg="#F2F2F2")
        max_label.config(bg="white", fg="black")
        max_entry.config(fg="black", bg="#F2F2F2")
        button_container.config(bg="white")
        confirm_button.config(style="Round.TButton")
        reset_button.config(style="Round.TButton")
        is_dark_mode = False
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900