Skip to content

Commit

Permalink
feat: Now virtual.Style can be customized more easily
Browse files Browse the repository at this point in the history
style: Improve code styles
  • Loading branch information
Xiaokang2022 committed Jan 11, 2025
1 parent e91664d commit 5e9ecec
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 114 deletions.
23 changes: 20 additions & 3 deletions tkintertools/core/virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,26 @@ def get_disabled_style(self, *, element: Element) -> dict[str, str]:

return now_style

def init(
self,
key: Element | str | int,
*,
theme: typing.Literal["light", "dark"] | None = None,
) -> None:
"""Initialize some style of an element.
* `name`: the key of the element
* `theme`: the theme name, None indicates both
"""
name = self._get_key(key)

if theme != "light":
if not self.dark.get(name):
self.dark[name] = {}
if theme != "dark":
if not self.light.get(name):
self.light[name] = {}

def get(
self,
*,
Expand Down Expand Up @@ -688,7 +708,6 @@ def __init__(
position: tuple[int, int] = (0, 0),
size: tuple[int, int] | None = None,
*,
name: str | None = None,
anchor: typing.Literal["n", "s", "w", "e", "nw", "ne", "sw", "se", "center"] = "nw",
capture_events: bool | None = None,
gradient_animation: bool | None = None,
Expand All @@ -699,7 +718,6 @@ def __init__(
* `master`: parent canvas
* `position`: position of the widget
* `size`: size of the widget
* `name`: name of the widget
* `anchor`: layout anchor of the widget
* `capture_events`: wether detect another widget under the widget
* `gradient_animation`: wether enable animation
Expand All @@ -719,7 +737,6 @@ def __init__(
self.position: tuple[float, float] = position
self.size: tuple[float, float] = (0, 0) if size is None else size

self.name = name
self.anchor = anchor

if capture_events is None and self.nested:
Expand Down
102 changes: 51 additions & 51 deletions tkintertools/standard/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,26 +240,26 @@ def set(
self,
theme: typing.Literal["light", "dark"] | None = None,
*,
bg: tuple[str | None, ...] | str | None = None,
ol: tuple[str | None, ...] | str | None = None,
bg_in: tuple[str | None, ...] | str | None = None,
ol_in: tuple[str | None, ...] | str | None = None,
bg_slot: tuple[str | None, ...] | str | None = None,
ol_slot: tuple[str | None, ...] | str | None = None,
bg_dot: tuple[str | None, ...] | str | None = None,
ol_dot: tuple[str | None, ...] | str | None = None,
) -> None:
"""Set the style of the widget.
* `theme`: the theme name, None indicates both
* `bg`: the background color of the widget.
* `ol`: the outline color of the widget.
* `bg_in`: the inside background color of the widget.
* `ol_in`: the inside outline color of the widget.
* `bg_slot`: the background color of the widget.
* `ol_slot`: the outline color of the widget.
* `bg_dot`: the inside background color of the widget.
* `ol_dot`: the inside outline color of the widget.
states: "normal-off", "hover-off", "active-off", "normal-on",
"hover-on", "active-on"
"""
self._set(theme, bg, fill=("Rectangle.out", "SemicircularRectangle"))
self._set(theme, ol, outline=("Rectangle.out", "SemicircularRectangle"))
self._set(theme, bg_in, fill=("Rectangle.in", "Oval"))
self._set(theme, ol_in, fill=("Rectangle.in", "Oval"))
self._set(theme, bg_slot, fill=("Rectangle.out", "SemicircularRectangle"))
self._set(theme, ol_slot, outline=("Rectangle.out", "SemicircularRectangle"))
self._set(theme, bg_dot, fill=("Rectangle.in", "Oval"))
self._set(theme, ol_dot, fill=("Rectangle.in", "Oval"))
self.widget.update()


Expand Down Expand Up @@ -320,22 +320,22 @@ def set(
fg: tuple[str | None, ...] | str | None = None,
bg: tuple[str | None, ...] | str | None = None,
ol: tuple[str | None, ...] | str | None = None,
hl: tuple[str | None, ...] | str | None = None,
bg_bar: tuple[str | None, ...] | str | None = None,
) -> None:
"""Set the style of the widget.
* `theme`: the theme name, None indicates both
* `fg`: the foreground color of the widget.
* `bg`: the background color of the widget.
* `ol`: the outline color of the widget.
* `hl`: the highlight bar of the widget
* `bg_bar`: the highlight bar of the widget (Only for Windows11 theme)
states: "normal", "hover", "active"
"""
self._set(theme, fg, fill="SingleLineText")
self._set(theme, bg, fill=("Rectangle", "RoundedRectangle.in"))
self._set(theme, ol, outline=("Rectangle", "RoundedRectangle.in"))
self._set(theme, hl, fill="RoundedRectangle.out", outline="RoundedRectangle.out")
self._set(theme, bg_bar, fill="RoundedRectangle.out", outline="RoundedRectangle.out")
self.widget.update()


Expand Down Expand Up @@ -539,25 +539,25 @@ def set(
self,
theme: typing.Literal["light", "dark"] | None = None,
*,
bg: tuple[str | None, ...] | str | None = None,
ol: tuple[str | None, ...] | str | None = None,
bg_in: tuple[str | None, ...] | str | None = None,
ol_in: tuple[str | None, ...] | str | None = None,
bg_box: tuple[str | None, ...] | str | None = None,
ol_box: tuple[str | None, ...] | str | None = None,
bg_dot: tuple[str | None, ...] | str | None = None,
ol_dot: tuple[str | None, ...] | str | None = None,
) -> None:
"""Set the style of the widget.
* `theme`: the theme name, None indicates both
* `bg`: the background color of the widget.
* `ol`: the outline color of the widget.
* `bg_in`: the inside background color of the widget.
* `ol_in`: the inside outline color of the widget.
* `bg_box`: the background color of the widget.
* `ol_box`: the outline color of the widget.
* `bg_dot`: the inside background color of the widget.
* `ol_dot`: the inside outline color of the widget.
states: "normal", "hover", "active"
"""
self._set(theme, bg, fill=0)
self._set(theme, ol, outline=0)
self._set(theme, bg_in, fill=1)
self._set(theme, ol_in, outline=1)
self._set(theme, bg_box, fill=0)
self._set(theme, ol_box, outline=0)
self._set(theme, bg_dot, fill=1)
self._set(theme, ol_dot, outline=1)
self.widget.update()


Expand Down Expand Up @@ -607,25 +607,25 @@ def set(
self,
theme: typing.Literal["light", "dark"] | None = None,
*,
bg: tuple[str | None, ...] | str | None = None,
ol: tuple[str | None, ...] | str | None = None,
bg_in: tuple[str | None, ...] | str | None = None,
ol_in: tuple[str | None, ...] | str | None = None,
bg_slot: tuple[str | None, ...] | str | None = None,
ol_slot: tuple[str | None, ...] | str | None = None,
bg_bar: tuple[str | None, ...] | str | None = None,
ol_bar: tuple[str | None, ...] | str | None = None,
) -> None:
"""Set the style of the widget.
* `theme`: the theme name, None indicates both
* `bg`: the background color of the widget.
* `ol`: the outline color of the widget.
* `bg_in`: the inside background color of the widget.
* `ol_in`: the inside outline color of the widget.
* `bg_slot`: the background color of the widget.
* `ol_slot`: the outline color of the widget.
* `bg_bar`: the inside background color of the widget.
* `ol_bar`: the inside outline color of the widget.
states: "normal", "hover"
"""
self._set(theme, bg, fill=0)
self._set(theme, ol, outline=0)
self._set(theme, bg_in, fill=1)
self._set(theme, ol_in, outline=1)
self._set(theme, bg_slot, fill=0)
self._set(theme, ol_slot, outline=0)
self._set(theme, bg_bar, fill=1)
self._set(theme, ol_bar, outline=1)
self.widget.update()


Expand Down Expand Up @@ -769,26 +769,26 @@ def set(
self,
theme: typing.Literal["light", "dark"] | None = None,
*,
fg: tuple[str | None, ...] | str | None = None,
bg: tuple[str | None, ...] | str | None = None,
pt: tuple[str | None, ...] | str | None = None,
hl: tuple[str | None, ...] | str | None = None,
fg_slot: tuple[str | None, ...] | str | None = None,
bg_slot: tuple[str | None, ...] | str | None = None,
bg_pnt: tuple[str | None, ...] | str | None = None,
bg_dot: tuple[str | None, ...] | str | None = None,
) -> None:
"""Set the style of the widget.
* `theme`: the theme name, None indicates both
* `fg`: the foreground color of the widget.
* `bg`: the background color of the widget.
* `pt`: the pointer color of the widget.
* `hl`: the pointer highlight part color of the widget (Only for
* `fg_slot`: the foreground color of the widget.
* `bg_slot`: the background color of the widget.
* `bg_pnt`: the pointer color of the widget.
* `bg_dot`: the pointer highlight part color of the widget (Only for
Windows11 theme).
states: "normal", "hover", "active"
"""
self._set(theme, fg, fill=1, outline=1)
self._set(theme, bg, fill=0, outline=0)
self._set(theme, pt, fill=2, outline=2)
self._set(theme, hl, fill="Oval.in", outline="Oval.in")
self._set(theme, fg_slot, fill=1, outline=1)
self._set(theme, bg_slot, fill=0, outline=0)
self._set(theme, bg_pnt, fill=2, outline=2)
self._set(theme, bg_dot, fill="Oval.in", outline="Oval.in")
# Only works on Windows11 theme, compatible with other themes
self.widget.update()

Expand Down
Loading

0 comments on commit 5e9ecec

Please sign in to comment.