Skip to content

Conversation

@alejandroautalan
Copy link

When a <<ThemeChanged>> occurs the style is recreated again.
This can slow down ui update if you have multiple DateEntry widgets and you change the style of another widget.

The following script tries to show how the style is re-created if I modify the style of another widget.

#!/usr/bin/python3
import tkinter as tk
import tkinter.ttk as ttk
from tkcalendar import Calendar as CalendarBase
from tkcalendar import DateEntry as DateEntryBase


class Calendar(CalendarBase):
    def _setup_style(self, event=None):
        print("Calendar: _setup_style() called for", self)
        super()._setup_style(event)


class DateEntry(DateEntryBase):
    def _setup_style(self, event=None):
        print("DateEntry: _setup_style() called for", self)
        super()._setup_style(event)  



class ThemeIssueDemoUI:
    def __init__(self, master=None, data_pool=None):
        # build ui
        tk1 = tk.Tk(master)
        tk1.configure(height=200, width=200)
        frame2 = ttk.Frame(tk1)
        frame2.configure(height=200, padding=5, width=200)
        frame3 = ttk.Frame(frame2)
        frame3.configure(height=200, width=200)
        label2 = ttk.Label(frame3)
        label2.configure(
            font="{Helvetica} 16 {}",
            style="Colored.TLabel",
            text='Colored Label')
        label2.pack(pady=40, side="top")
        button1 = ttk.Button(frame3)
        button1.configure(text='Change label color')
        button1.pack(side="top")
        button1.configure(command=self.on_change_label_color)
        frame3.pack(expand=True, side="bottom")
        label1 = ttk.Label(frame2)
        label1.configure(text='Theme:')
        label1.pack(side="left")
        self.cb_theme = ttk.Combobox(frame2, name="cb_theme")
        self.theme_var = tk.StringVar()
        self.cb_theme.configure(textvariable=self.theme_var)
        self.cb_theme.pack(side="left")
        self.cb_theme.bind(
            "<<ComboboxSelected>>",
            self.on_theme_changed,
            add="")
        frame2.pack(expand=True, fill="both", side="left")
        frame1 = ttk.Frame(tk1)
        frame1.configure(height=200, padding=5, width=200)
        calendar1 = Calendar(frame1)
        calendar1.pack(side="top")
        dateentry1 = DateEntry(frame1)
        dateentry1.pack(side="top")
        frame1.pack(side="left")

        # Main widget
        self.mainwindow = tk1

    def run(self):
        self.mainwindow.mainloop()

    def on_change_label_color(self):
        pass

    def on_theme_changed(self, event=None):
        pass


class ThemeIssueDemo(ThemeIssueDemoUI):
    def __init__(self, master=None):
        super().__init__(master)
        
        self.style = ttk.Style(self.mainwindow)
        themes = self.style.theme_names()
        self.cb_theme.configure(values=themes)
        self.theme_var.set(self.style.theme_use())
    
    def on_theme_changed(self, event=None):
        value = self.cb_theme.get()
        print(f"Using theme {value}")
        self.style.theme_use(value)    

    def on_change_label_color(self):
        print("Label color change.")
        self.style.configure("Colored.TLabel", foreground="green")


if __name__ == "__main__":
    app = ThemeIssueDemo()
    app.run()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant