【问题标题】:Getting value from slider to display on a different screen (python, kivy)从滑块获取值以显示在不同的屏幕上(python,kivy)
【发布时间】:2018-01-10 08:04:34
【问题描述】:

我正在尝试在不同的屏幕上显示滑块的值。我已经尝试过这个(下面的代码),但由于某种原因,该值似乎没有出现。代码运行良好,但没有返回值。感谢您的帮助:) 干杯。

温度屏幕

这是python代码的sn-p:

class Thermostat(Screen):
label = StringProperty()
    def display(self):
        tempVal = self.label 
        return str(tempVal)

还有 kv 文件:

<Thermostat>:
name: "thermostat"
BoxLayout:
    orientation: 'horizontal'
    cols: 2

    Label:
        id: label
        font_size: "11sp"
        text: "INSIDE: " + root.display()

    Label:
        text: "More Info"
        font_size: "11sp"

kv 文件 2:此屏幕保存来自滑块的真实值,我正在尝试将该值传递到恒温器屏幕。

<Temperature>:
BoxLayout:
    size_hint_y: None
    height: '48dp'
    cols: 3

    Label:
        text: 'THERMOSTAT'

    Slider:
        id: temp
        min: 40
        max: 100
        value: 1
        step: 1
        on_value: app.root.get_screen('thermostat').label = str('{}'.format(temp.value))    

    Label:
        id: slide_val
        text: '{}'.format(temp.value)

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    root.display 只在程序开始时被调用一次。为使其正常工作,每次更改滑块的值时都应调用 root.display

    但是,在 kv languaje 中使用propertis 非常简单:

    from kivy.app import App
    from kivy.uix.screenmanager import Screen, ScreenManager
    from kivy.lang.builder import Builder
    
    
    
    Builder.load_string('''
    
    <Manager>:
        id: manager
        Thermostat:
            id: thermostat
            name: 'thermostat'
            manager: 'screen_manager'
            temp: temperature.temp                    #<<<<<<<<<<<<
    
        Temperature:
            id: temperature
            name: 'temperature'
            manager: 'screen_manager'
    
    
    <Thermostat>:
        temp: 0                                       #<<<<<<<<<<<<
        BoxLayout:
            orientation: 'horizontal'
            cols: 3
    
            Label:
                id: label
                font_size: "11sp"
                text: "INSIDE: {}".format(root.temp)  #<<<<<<<<<<<<
    
            Label:
                text: "More Info"
                font_size: "11sp"
    
            Button:
                text: ">"
                on_release: app.root.current= "temperature"
                size_hint_x: None
                width: 30
    
    
    
    <Temperature>:
        temp: temp_slider.value                              #<<<<<<<<<<<<
        BoxLayout:
            cols: 4
    
            Button:
                text: "<"
                on_press: app.root.current = "thermostat"
                size_hint_x: None
                width: 30
    
            Label:
                text: 'THERMOSTAT'
    
            Slider:
                id: temp_slider
                min: 40
                max: 100
                value: 40
                step: 1
    
            Label:
                id: slide_val
                text: str(root.temp)
    
    ''')
    
    
    class Thermostat(Screen):
        pass
    
    
    class Temperature(Screen):
        pass
    
    
    class Manager(ScreenManager):
        pass
    
    
    class ExampleApp(App):
        def build(self):
            return Manager()
    
    
    if __name__ == "__main__":
        ExampleApp().run()
    

    如果您想在您的类Temperature 中使用滑块的值,只需在类中声明该属性即可:

    from kivy.properties import NumericProperty
    
    
    class Temperature(Screen):
        temp = NumericProperty()
        def __init__(self, **kwargs):
            super(Temperature, self).__init__(**kwargs)
    

    【讨论】:

      猜你喜欢
      • 2018-11-14
      • 1970-01-01
      • 2015-06-07
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      相关资源
      最近更新 更多