从滑块获取值以在不同的屏幕上显示(python,kivy)

时间:2018-01-09 20:46:00

标签: python kivy kivy-language

我试图在不同的屏幕上显示滑块的值。我试过这个(下面的代码),但由于某种原因,价值似乎没有显示出来。代码运行正常但没有返回值。谢谢你的帮助:)干杯。

温度屏幕

这是python代码的片段:

function namesToId(element) {
    if(element===undefined)element='body';
    $(element).find('[name]').each(function(index, element){
        var elementId = $(this).attr('id');
        var elementName = $(this).attr('name');
        if(elementId===undefined){
            if(elementName.search(/\[]/)===-1){
                $(this).attr('id', elementName);
            }else{
                $(this).attr('id', elementName.replace(/\[]/, '_'+name="'+elementName+'"][id] ').length));
            }
        }
    });
};

和kv文件:

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

kv file 2:此屏幕保存滑块的真值,我试图将该值传递给Thermostat屏幕。

<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"

1 个答案:

答案 0 :(得分:1)

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

然而,使用kv languaje中的属性来实现这一点非常简单:

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)

enter image description here