Skip to content

Commit

Permalink
feat: widget SpinBox adds parameters format and step to control…
Browse files Browse the repository at this point in the history
… the format of the value and the size of the single change, respectively
  • Loading branch information
Xiaokang2022 committed Nov 14, 2024
1 parent 8683896 commit b0a2192
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions tkintertools/standard/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,8 @@ def __init__(
position: tuple[int, int],
size: tuple[int, int] | None = None,
*,
format: str = "d",
step: int = 1,
family: str | None = None,
fontsize: int | None = None,
weight: typing.Literal['normal', 'bold'] = "normal",
Expand All @@ -1101,6 +1103,8 @@ def __init__(
* `master`: parent canvas
* `position`: position of the widget
* `size`: size of the widget
* `format`: format of value
* `step`: value of each change
* `family`: font family
* `fontsize`: font size
* `weight`: weight of the text
Expand Down Expand Up @@ -1142,17 +1146,19 @@ def __init__(
Button(self, (size[0]-w-4, size[1]/2 + 2), (w, h), text="▼",
fontsize=14, through=True, command=lambda:
command(False) if command is not None else self.change(False))
self.format = format
self.step = step
features.SpinBoxFeature(self, command=command)

def change(self, up: bool) -> None:
"""Try change the current value"""
if not (value := self.widgets[0].get()):
return self.widgets[0].set("0")
return self.widgets[0].set(("%"+self.format) % 0)
try:
value = float(value) + (1 if up else -1)
value = float(value) + (self.step if up else -self.step)
if math.isclose(value, int_value := int(value)):
value = int_value
self.widgets[0].set(str(value))
self.widgets[0].set(("%"+self.format) % value)
except ValueError:
pass

Expand Down

0 comments on commit b0a2192

Please sign in to comment.