从另一个屏幕更改Kivy滑块值

时间:2018-10-04 06:39:31

标签: slider kivy

我正在尝试将滑块的值重置为3。我有两个滑块,其值分别为1-5和1。

我最初将值设置为3。我希望能够使用滑块,进入主屏幕,然后将它们设置回3。重置后,滑块需要重新使用,这是当前的我坚持的问题。

每次我按下“重置”按钮时,滑块会返回到3,但是无法再次移动...有人有见识吗?

简单的代码示例:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.slider import Slider

Builder.load_string('''

<HomeScreen>:
    BoxLayout:
        orientation: "vertical"
        Button:
            text: "Next Screen"
            on_release:
                app.root.current = "screen1"
        Button:
            text: "If I press this, it will turn the values back to 3"
            on_release:
                app.root.get_screen("screen1").slider1.value = 3
                app.root.get_screen("screen2").slider2.value = 3

<Screen1>:
    BoxLayout:
        orientation: "vertical"
        Label:
            text: str(slider1.value)
        Slider:
            id: slider1
            max: 5
            min: 1
            value: 3
            step: 1

        Button:
            text: "Next Screen"
            on_release:
                app.root.current = "screen2"
<Screen2>:
    BoxLayout:
        orientation: "vertical"
        Label:
            text: str(slider2.value)
        Slider:
            id: slider2
            max: 5
            min: 1
            value: 3
            step: 1
        Button:
            text: "Back to Home Screen"
            on_release:
                app.root.current = "homescreen"

<SM>:
    name: "sm"
    id: sm
    HomeScreen:
        name: "homescreen"
        id: hs
    Screen1:
        name: "screen1"
        id: s1
    Screen2:
        name: "screen2"
        id: s2

''')

class HomeScreen(Screen):
    pass

class Screen1(Screen):
    slider1 = Slider

class Screen2(Screen):
    slider2 = Slider

class SM(ScreenManager):
    pass

class MyApp(App):
    def build(self):
        global sm
        sm = SM()
        return sm

if __name__ == "__main__":
    MyApp().run()

1 个答案:

答案 0 :(得分:0)

首先,您不需要这些作业

KeyEventArgs.DeviceId, KeyRoutedEventArgs.DeviceId, Windows.UI.Core.CoreWindow.GetForCurrentThread().GetCurrentKeyEventDeviceId()

因为您将新的滑块分配给了滑块变量。

要访问滑块值,您必须使用如下ID:

class Screen1(Screen):
    slider1 = Slider

class Screen2(Screen):
    slider2 = Slider

仅此而已...