Я создал программное обеспечение для управления тренажерным залом с помощью Tkinter и использую собственные темы (темный лес и светлый лес) для пользовательского интерфейса. Я использую OptionMenu, чтобы позволить пользователям динамически переключаться между этими темами. Смена темы работает в первый раз (с темной на светлую или наоборот), но когда я пытаюсь переключиться обратно на исходную тему (например, со светлой на темную), пользовательский интерфейс не обновляется соответствующим образом.Вот упрощенная версия моего кода:
class GymManagementApp(tk.Tk):
def __init__(self):
super().__init__()
# App configuration
self.title("Gym Management Software")
self.geometry("800x800")
self.style = ttk.Style()
# Load the initial theme from environment
self.theme = get_env("THEME")
if self.theme is None:
theme_path = os.path.join(os.getcwd(), "theme/forest-dark.tcl")
set_env("THEME", "forest-dark") # set default theme
else:
theme_path = os.path.join(os.getcwd(), f"theme/{self.theme}.tcl")
# Load and apply the theme
self.tk.call("source", theme_path)
self.style.theme_use(self.theme)
def update_theme(self, theme):
"""Switch the application's theme."""
print(f"Theme changed to: {theme}")
print(self.style.theme_names())
# Load the new theme if not already loaded
theme_path = os.path.join(os.getcwd(), f"theme/{theme}.tcl")
if theme not in self.style.theme_names():
print(f"Loading theme: {theme}")
self.tk.call("source", theme_path)
# Apply the new theme
self.style.theme_use(theme)
# Store the new theme in the environment variable
self.theme = theme
set_env("THEME", theme)
# Ensure the UI is updated immediately
self.update_idletasks()
Вывод:
Когда я меняю тему в первый раз, все работает нормально:
Theme changed to: Light
('clam', 'alt', 'default', 'forest-dark', 'classic')
Loading theme: forest-light
Но когда я пытаюсь вернуться к предыдущей теме, пользовательский интерфейс не обновляется:
Theme changed to: Dark
('forest-light', 'clam', 'alt', 'default', 'forest-dark', 'classic')
Подробнее здесь: https://stackoverflow.com/questions/791 ... in-tkinter
Тема не переключается взад и вперед в tkinter ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение