Skip to content

Commit

Permalink
feat: add mathod is_nested to class virtual.Widget and improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaokang2022 committed Nov 28, 2024
1 parent ec1aa00 commit d26dbcd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
8 changes: 6 additions & 2 deletions tkintertools/core/virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ def __init__(
name: str | None = None,
state: str = "normal",
anchor: typing.Literal["n", "s", "w", "e", "nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = True,
) -> None:
"""
Expand All @@ -463,7 +463,7 @@ def __init__(
self.name = name
self.state = state
self.anchor = anchor
self.through = through
self.through = self.is_nested() if through is None else through
self.animation = animation

self.widgets: list[Widget] = []
Expand Down Expand Up @@ -496,6 +496,10 @@ def offset(self) -> tuple[float, float]:
case _: _offset = self.size[0]/2, self.size[1]/2
return _offset

def is_nested(self) -> bool:
"""Whether the widget is a nested widget"""
return self.master is not None

def register(self, component: Component) -> None:
"""Register a component to the widget"""
if isinstance(component, Shape):
Expand Down
40 changes: 20 additions & 20 deletions tkintertools/standard/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(
justify: typing.Literal["left", "center", "right"] = "left",
name: str | None = None,
anchor: typing.Literal["n", "e", "w", "s", "nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -100,7 +100,7 @@ def __init__(
image: enhanced.PhotoImage | None = None,
name: str | None = None,
anchor: typing.Literal["n", "e", "w", "s", "nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -160,7 +160,7 @@ def __init__(
image: enhanced.PhotoImage | None = None,
name: str | None = None,
anchor: typing.Literal["n", "e", "w", "s", "nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -220,7 +220,7 @@ def __init__(
image: enhanced.PhotoImage | None = None,
name: str | None = None,
anchor: typing.Literal["n", "e", "w", "s", "nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -274,7 +274,7 @@ def __init__(
image: enhanced.PhotoImage | None = None,
name: str | None = None,
anchor: typing.Literal["n", "e", "w", "s", "nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -345,7 +345,7 @@ def __init__(
image: enhanced.PhotoImage | None = None,
name: str | None = None,
anchor: typing.Literal["n", "e", "w", "s", "nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -429,7 +429,7 @@ def __init__(
image: enhanced.PhotoImage | None = None,
name: str | None = None,
anchor: typing.Literal["n", "e", "w", "s", "nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -496,7 +496,7 @@ def __init__(
image: enhanced.PhotoImage | None = None,
name: str | None = None,
anchor: typing.Literal["n", "e", "w", "s", "nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -565,7 +565,7 @@ def __init__(
image: enhanced.PhotoImage | None = None,
name: str | None = None,
anchor: typing.Literal["n", "e", "w", "s", "nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -630,7 +630,7 @@ def __init__(
image: enhanced.PhotoImage | None = None,
name: str | None = None,
anchor: typing.Literal["n", "e", "w", "s", "nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -713,7 +713,7 @@ def __init__(
name: str | None = None,
anchor: typing.Literal["n", "e", "w", "s",
"nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = False,
) -> None:
"""
Expand Down Expand Up @@ -766,7 +766,7 @@ def __init__(
image: enhanced.PhotoImage | None = None,
name: str | None = None,
anchor: typing.Literal["n", "e", "w", "s", "nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -820,7 +820,7 @@ def __init__(
image: enhanced.PhotoImage | None = None,
name: str | None = None,
anchor: typing.Literal["n", "e", "w", "s", "nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -875,7 +875,7 @@ def __init__(
command: collections.abc.Callable[[float], typing.Any] | None = None,
name: str | None = None,
anchor: typing.Literal["n", "e", "w", "s", "nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -957,7 +957,7 @@ def __init__(
layout: typing.Literal["horizontal", "vertical"] = "horizontal",
name: str | None = None,
anchor: typing.Literal["n", "e", "w", "s", "nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -1012,7 +1012,7 @@ def __init__(
ToggleButton(
self, position, size, text=text, family=family, fontsize=fontsize, weight=weight,
slant=slant, underline=underline, overstrike=overstrike, justify=justify,
animation=animation, image=image, through=True,
animation=animation, image=image,
command=lambda _, i=i: (self.set(i), command(i) if command else None))
total_side_length += size[layout == "vertical"] + 5
self.command = command
Expand Down Expand Up @@ -1058,7 +1058,7 @@ def __init__(
image: enhanced.PhotoImage | None = None,
name: str | None = None,
anchor: typing.Literal["n", "e", "w", "s", "nw", "ne", "sw", "se", "center"] = "nw",
through: bool = False,
through: bool | None = None,
animation: bool = True,
) -> None:
"""
Expand Down Expand Up @@ -1093,12 +1093,12 @@ def __init__(
InputBox(
self, (0, 0), size, family=family, fontsize=fontsize, weight=weight, slant=slant,
underline=underline, overstrike=overstrike, align=align, placeholder=placeholder,
show=show, limit=limit, image=image, animation=animation, through=True)
show=show, limit=limit, image=image, animation=animation)
h = size[1]/2 - 6
w = h/configs.Constant.GOLDEN_RATIO if configs.Env.system == "Windows10" else 2*h
Button(self, (size[0]-w-4, 4), (w, h), text="▲", fontsize=14, through=True,
Button(self, (size[0]-w-4, 4), (w, h), text="▲", fontsize=14,
command=lambda: command(True) if command is not None else self.change(True))
Button(self, (size[0]-w-4, size[1]/2 + 2), (w, h), text="▼", fontsize=14, through=True,
Button(self, (size[0]-w-4, size[1]/2 + 2), (w, h), text="▼", fontsize=14,
command=lambda: command(False) if command is not None else self.change(False))
self.format = style
self.step = step
Expand Down

0 comments on commit d26dbcd

Please sign in to comment.