kivy-garden / garden.knob

Knob widget for Kivy.
MIT License
36 stars 9 forks source link

Add Label #3

Open tomdertech opened 7 years ago

tomdertech commented 7 years ago

Since show_label has been removed, please could someone provide an example of how to overlay a label to show the value? I cannot figure it out - Thank you.

SpotlightKid commented 7 years ago

This works for me:

Knob:
    min: 0
    max: 100
    size: 100, 100
    value: 0
    show_marker: False
    knobimg_source: "img/knob_metal.png"

    Label:
        text: "{}".format(int(self.parent.value))
        center: self.parent.center
        font_size: 20
        color: 0.1, 0.1, 0.1, 1
labF212 commented 1 year ago

https://github.com/labF212/Gui-for-Python-KivyMD/tree/img

from kivy.app import App from kivy.uix.gridlayout import GridLayout from kivy.lang import Builder from kivy.garden.knob import Knob from kivy.properties import StringProperty, NumericProperty

class KnobWithLabel(Knob): label_text = StringProperty("") voltage = NumericProperty(0)

def __init__(self, **kwargs):
    super(KnobWithLabel, self).__init__(**kwargs)
    self.bind(value=self.update_label_text)
    self.update_label_text()

def update_label_text(self, *args):
    voltage = round(self.value * 5 / 360, 2)
    self.voltage = voltage
    self.label_text = f"{voltage}V"

LOAD KV UIX

KV = '''

: GridLayout: pos: root.pos size: root.size cols: 3 spacing: 100 padding: 50 KnobWithLabel: min:0 max:360 size: 200, 200 value: 0 show_marker: True knobimg_source: "knob_metal.png" show_marker: False : label_text: "0V" Label: text: root.label_text center: self.parent.center font_size: 20 color: 0.1, 0.1, 0.1, 1 ''' Builder.load_string(KV) class Main(GridLayout): pass class MyApp(App): title = "Fancy Buttons" def build(self): root_widget = Main() return root_widget if __name__ == '__main__': MyApp().run()
labF212 commented 1 year ago

The previous code convert the Label 0-360º to 0-5Vdc. Use as you wish.