Skip to content

Commit

Permalink
Update ThemedTk for #50
Browse files Browse the repository at this point in the history
  • Loading branch information
RedFantom committed Mar 4, 2019
1 parent c62d995 commit 3d74923
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
4 changes: 2 additions & 2 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Example(ThemedTk):
Example that is used to create screenshots for new themes.
"""
def __init__(self):
ThemedTk.__init__(self)
ThemedTk.__init__(self, themebg=True)
# Create widgets
self.menu = tk.Menu(self, tearoff=False)
self.sub_menu = tk.Menu(self.menu, tearoff=False)
Expand Down Expand Up @@ -101,5 +101,5 @@ def screenshot_themes(self, *args):

if __name__ == '__main__':
example = Example()
example.set_theme("scidblue")
example.set_theme("equilux")
example.mainloop()
5 changes: 5 additions & 0 deletions ttkthemes/_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ def themes(self):
"""Property alias of get_themes()"""
return self.get_themes()

@property
def current_theme(self):
"""Property to get the currently enabled theme"""
return self.tk.eval("return $ttk::currentTheme")

def set_theme_advanced(self, theme_name, brightness=1.0,
saturation=1.0, hue=1.0,
preserve_transparency=True, output_dir=None,
Expand Down
58 changes: 50 additions & 8 deletions ttkthemes/themed_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ class ThemedTk(tk.Tk, ThemedWidget):
used as a drop-in replacement for the normal Tk class. Additional
options:
- Toplevel background color:
- Initial theme ``theme``:
Sets the initial theme to the theme specified. If the theme is
not available, fails silenty (there is no indication that the
theme is not set other than
- Toplevel background color ``toplevel``:
Hooks into the Toplevel.__init__ function to set a default window
background color in the options passed. The hook is not removed
after the window is destroyed, which is by design because creating
multiple Tk instances should not be done in the first place.
- Tk background color:
- Tk background color ``themebg``:
Simply sets the background color of the Tkinter window to the
default TFrame background color specified by the theme.
"""
Expand All @@ -34,31 +39,68 @@ def __init__(self, *args, **kwargs):
:param background: Control Tk background color option
"""
theme = kwargs.pop("theme", None)
toplevel = kwargs.pop("toplevel", False)
background = kwargs.pop("background", False)
self._toplevel = kwargs.pop("toplevel", None)
self._themebg = kwargs.pop("themebg", None)
gif_override = kwargs.pop("gif_override", False)
# Initialize as tk.Tk
tk.Tk.__init__(self, *args, **kwargs)
# Initialize as ThemedWidget
ThemedWidget.__init__(self, self.tk, gif_override)
# Set initial theme
if theme is not None and theme in self.get_themes():
self.set_theme(theme, toplevel, background)
self.set_theme(theme, self._toplevel, self._themebg)
self.__init__toplevel = tk.Toplevel.__init__

def set_theme(self, theme_name, toplevel=False, background=False):
def set_theme(self, theme_name, toplevel=None, themebg=None):
"""Redirect the set_theme call to also set Tk background color"""
if self._toplevel is not None and toplevel is None:
toplevel = self._toplevel
if self._themebg is not None and themebg is None:
themebg = self._themebg
ThemedWidget.set_theme(self, theme_name)
color = ttk.Style(self).lookup("TFrame", "background", default="white")
if background is True:
color = self._get_bg_color()
if themebg is True:
self.config(background=color)
if toplevel is True:
self._setup_toplevel_hook(color)

def _get_bg_color(self):
return ttk.Style(self).lookup("TFrame", "background", default="white")

def _setup_toplevel_hook(self, color):
"""Setup Toplevel.__init__ hook for background color"""
def __toplevel__(*args, **kwargs):
kwargs.setdefault("background", color)
self.__init__toplevel(*args, **kwargs)

tk.Toplevel.__init__ = __toplevel__

def config(self, kw=None, **kwargs):
"""Configure redirect to support additional options"""
background = kwargs.pop("themebg", self._themebg)
toplevel = kwargs.pop("toplevel", self._toplevel)
theme = kwargs.pop("theme", self.current_theme)
color = self._get_bg_color()
if background != self._themebg:
self.configure(bg="white")
if toplevel != self._toplevel:
if toplevel is True:
self._setup_toplevel_hook(color)
else:
tk.Toplevel.__init__ = self.__init__toplevel
if theme != self.current_theme:
self.set_theme(theme)
return tk.Tk.config(self, kw, **kwargs)

def configure(self, kw=None, **kwargs):
return self.config(kw, **kwargs)

def cget(self, k):
"""cget redirect to support additional options"""
if k == "themebg":
return self._themebg
elif k == "toplevel":
return self._toplevel
elif k == "theme":
return self.current_theme
return tk.Tk.cget(self, k)

0 comments on commit 3d74923

Please sign in to comment.