How To Allow Infinite Integer Values In A Spinner?
Solution 1:
It seems like kivy at the moment does not provide anything similar to a Spinner
or SpinBox
or however you want to call it. A widget that might be used instead is the Slider
but it looks awful and it's not so useful if you want to allow a very big range but with a small step.
Therefore I wrote my own implementation of a SpinBox
:
classSpinBox(BoxLayout):
"""A widget to show and take numeric inputs from the user.
:param min_value: Minimum of the range of values.
:type min_value: int, float
:param max_value: Maximum of the range of values.
:type max_value: int, float
:param step: Step of the selection
:type step: int, float
:param value: Initial value selected
:type value: int, float
:param editable: Determine if the SpinBox is editable or not
:type editable: bool
"""
min_value = NumericProperty(float('-inf'))
max_value = NumericProperty(float('+inf'))
step = NumericProperty(1)
value = NumericProperty(0)
range = ReferenceListProperty(min_value, max_value, step)
def__init__(self, btn_size_hint_x=0.2, **kwargs):
super(SpinBox, self).__init__(orientation='horizontal', **kwargs)
self.value_label = Label(text=str(self.value))
self.inc_button = TimedButton(text='+')
self.dec_button = TimedButton(text='-')
self.inc_button.bind(on_press=self.on_increment_value)
self.inc_button.bind(on_time_slice=self.on_increment_value)
self.dec_button.bind(on_press=self.on_decrement_value)
self.dec_button.bind(on_time_slice=self.on_decrement_value)
self.buttons_vbox = BoxLayout(orientation='vertical',
size_hint_x=btn_size_hint_x)
self.buttons_vbox.add_widget(self.inc_button)
self.buttons_vbox.add_widget(self.dec_button)
self.add_widget(self.value_label)
self.add_widget(self.buttons_vbox)
defon_increment_value(self, btn_instance):
iffloat(self.value) + float(self.step) <= self.max_value:
self.value += self.step
defon_decrement_value(self, btn_instance):
iffloat(self.value) - float(self.step) >= self.min_value:
self.value -= self.step
defon_value(self, instance, value):
instance.value_label.text = str(value)
Actually the code I use is slightly different because I think it is ugly to subclass a layout to implement a widget and thus I subclassed Widget
and added a horizontal BoxLayout
as only children of the Widget
, then I bind
ed every size and position change to update the size and position of this child(see this question for why I had to do that).
The TimedButton
is a subclass of Button
that allows long-presses and, when long-pressed, emits a on_time_slice
event every a certain amount of millisecond(thus the user will be able to hold the button to do a continuous increment). You can simply use a normal Button
if you want, removing the bind
s to on_time_slice
event.
The TimedButton
source code is this:
classTimedButton(Button):
"""A simple ``Button`` subclass that produces an event at regular intervals
when pressed.
This class, when long-pressed, emits an ``on_time_slice`` event every
``time_slice`` milliseconds.
:param long_press_interval: Defines the minimum time required to consider
the press a long-press.
:type long_press_interval: int
:param time_slice: The number of milliseconds of each slice.
:type time_slice: int
"""def__init__(self, long_press_interval=550, time_slice=225, **kwargs):
super(TimedButton, self).__init__(**kwargs)
self.long_press_interval = long_press_interval
self.time_slice = time_slice
self._touch_start = None
self._long_press_callback = None
self._slice_callback = None
self.register_event_type('on_time_slice')
self.register_event_type('on_long_press')
defon_state(self, instance, value):
if value == 'down':
start_time = time.time()
self._touch_start = start_time
defcallback(dt):
self._check_long_press(dt)
Clock.schedule_once(callback, self.long_press_interval / 1000.0)
self._long_press_callback = callback
else:
end_time = time.time()
delta = (end_time - (self._touch_start or0)) * 1000
Clock.unschedule(self._slice_callback)
# Fixes the bug of multiple presses causing fast increase
Clock.unschedule(self._long_press_callback)
if (self._long_press_callback isnotNoneand
delta > self.long_press_interval):
self.dispatch('on_long_press')
self._touch_start = None
self._long_press_callback = self._slice_callback = Nonedef_check_long_press(self, dt):
delta = dt * 1000if delta > self.long_press_interval and self.state == 'down':
self.dispatch('on_long_press')
self._long_press_callback = Nonedefslice_callback(dt):
self.dispatch('on_time_slice')
return self.state == 'down'
Clock.schedule_interval(slice_callback, self.time_slice / 1000.0)
self._slice_callback = slice_callback
defon_long_press(self):
passdefon_time_slice(self):
pass
Note that I had to bind the state
property instead of using on_touch_down
and on_touch_up
because they give some strange behaviour, and even when "working" there were some strange things happening by no reason(e.g. clicking the decrement button caused on_increment
to be called even though the bind
ings where correct).
Edit: Updated the TimedButton
class fixing a little bug(the previous implementation when clicked rapidly multiple times and then holding down the button would yield too many on_time_slice
events: I'd forgot to "unschedule" the _long_press_callback
when the state goes 'normal'
Post a Comment for "How To Allow Infinite Integer Values In A Spinner?"