I have a project I would like to try and use kivy as a user interface.
The interface would have 1 slider 2 buttons and a text box. Basically the slider can be moved by sliding or finely incremented by the buttons. With a text box to read out the values but also able to manual enter a desired value.
I have googled around a bit and have not seen any examples of bonding all 3 inputs to 1 slider. I have only seen taking values from a slider and displaying it, I have no idea how to move a slider with a value.
main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang.builder import Builder
class CustomSlider(BoxLayout):
def on_kv_post(self, base_widget):
super().on_kv_post(base_widget)
self.ids["button_less"].bind(on_press=self.on_button_less_click)
self.ids["button_more"].bind(on_press=self.on_button_more_click)
self.ids["text_input"].bind(text=self.on_text_input_changement)
self.ids["slider"].bind(on_touch_move=self.on_slider_move)
def on_slider_move(self, w, touch):
self.ids["slider"].value = 5 * round(self.ids["slider"].value / 5)
self.ids["text_input"].text = str(self.ids["slider"].value)
def on_button_less_click(self, w):
self.ids["text_input"].text = str(int(self.ids["text_input"].text) - 5)
def on_button_more_click(self, w):
self.ids["text_input"].text = str(int(self.ids["text_input"].text) + 5)
def on_text_input_changement(self, w, value):
self.ids["text_input"].text = self.get_value(value)
self.ids["slider"].value = int(self.ids["text_input"].text)
def get_value(self, value):
if not self.isInt(value):
return "0"
value = int(value)
if value < 0: return "0"
elif value > 100: return "100"
else: return str(value)
def isInt(self, value):
try:
int(value)
return True
except:
return False
class MyFirstKivyApp(App):
def build(self):
return Builder.load_file("main.kv")
MyFirstKivyApp().run()
main.kv
<CustomSlider>:
orientation: "vertical"
BoxLayout:
Slider:
id: slider
min: 0
max: 100
BoxLayout:
Button:
id: button_less
text: "-"
Button:
id: button_more
text: "+"
BoxLayout:
TextInput:
id: text_input
input_filter: "int"
text: "0"
CustomSlider: