Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add step to Slider, + tests + docs #511

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions docs-src/docs/slider.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ __init__(
visible=True,
enabled=None,
width=None,
height=None)
height=None,
step=1)
```

### What is it?
Expand All @@ -33,7 +34,7 @@ slider = Slider(app)
app.display()
```

### Starting paramters
### Starting parameters

When you create a `Slider` object, you **must** specify a `master` and you can specify any of the the optional parameters. Specify parameters in the brackets, like this: `slider = Slider(app, horizontal=False)`

Expand All @@ -50,6 +51,7 @@ When you create a `Slider` object, you **must** specify a `master` and you can s
| enabled | boolean | None | No | If the widget should be enabled. If `None` (the default) the enabled property will be inherited from the master |
| width | [size](size.md) | None | No | Set the width of the widget in pixels or to `"fill"` |
| height | [size](size.md) | None | No | Set the height of the widget in pixels or to `"fill"` |
| step | int | 1 | No | The increment of the slider's value |


### Methods
Expand Down Expand Up @@ -93,6 +95,7 @@ You can set and get the following properties:
| value | string | The current value of the slider |
| visible | boolean | If this widget is visible |
| width | [size](size.md) | Set the width of the widget in pixels or to `"fill"` |
| step | int | The increment of the slider's value |

### Examples

Expand Down
13 changes: 11 additions & 2 deletions guizero/Slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ def __init__(
visible=True,
enabled=None,
width=None,
height=None):
height=None,
step=1):

# Set the direction
self._horizontal = horizontal
orient = HORIZONTAL if horizontal else VERTICAL

# Create a tk Scale object within this object
tk = Scale(master.tk, from_=start, to=end, orient=orient, command=self._command_callback)
tk = Scale(master.tk, from_=start, to=end, resolution=step, orient=orient, command=self._command_callback)

super().__init__(master, tk, grid, align, visible, enabled, width, height)

Expand Down Expand Up @@ -57,6 +58,14 @@ def end(self):
def end(self, value):
self._set_tk_config("to", value)

@property
def step(self):
return self._get_tk_config("resolution")

@step.setter
def step(self, value):
self._set_tk_config("resolution", value)

# Resize
def resize(self, width, height):
self._set_width(width)
Expand Down
23 changes: 15 additions & 8 deletions tests/test_slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,30 @@
auto_layout_test
)

def test_start_end_change():
def test_start_end_step_change():
a = App()
s = Slider(a)
assert s.tk.cget("from") == 0
assert s.tk.cget("to") == 100
s.start = 1
assert s.tk.cget("from") == 1
s.end = 99
assert s.tk.cget("to") == 99
assert s.tk.cget("resolution") == 1
s.start = 20
assert s.tk.cget("from") == 20
s.end = 90
assert s.tk.cget("to") == 90
s.step = 10
assert s.tk.cget("resolution") == 10
# Also check that getters have worked
assert s.start == 1
assert s.end == 99
assert s.start == 20
assert s.end == 90
assert s.step == 10
a.destroy()

def test_default_values():
a = App()
s = Slider(a)
assert s.tk.cget("from") == 0
assert s.tk.cget("to") == 100
assert s.tk.cget("resolution") == 1
assert s.tk.cget("orient") == "horizontal"
assert s.master == a
assert s.grid == None
Expand All @@ -53,7 +58,8 @@ def test_alt_values():
grid = [0,1],
align = "top",
width=10,
height=11)
height=11,
step=5)

assert s.tk.cget("from") == 10
assert s.tk.cget("to") == 20
Expand All @@ -63,6 +69,7 @@ def test_alt_values():
assert s.align == "top"
assert s.width == 10
assert s.height == 11
assert s.tk.cget("resolution") == 5
a.destroy()

def test_getters_setters():
Expand Down