需要将变量传递给新屏幕,然后读取并使用它

时间:2017-10-24 06:22:18

标签: python python-3.x kivy kivy-language

如何根据我使用Kivy和Python按下的按钮更改第二个屏幕的颜色/图像?这是解释我的问题的代码。

的Python:

startvalue = 0


class Start(Screen):
    pass


class End(Screen):
    global startvalue

    def which_color(self):
        if startvalue == 1:
            self.bg_image.source = 'blue.png'
        elif startvalue == 2:
            self.bg_image.source = 'red.png'
        elif startvalue == 3:
            self.bg_image.source = 'yellow.jpg'
        elif startvalue == 4:
            self.bg_image.source = 'green.jpg'

Kivy:

#: import startvalue __kivyseeing__.startvalue

ScreenManagement:
    Start:
    End:

<Start>:
    startvalue: startvalue
    id: start
    name: 'start'

    BoxLayout:

        Button:
            name: 'btn1'
            id: btn1
            text: "btn1"
            on_release:
                startvalue = 1
                app.root.current = 'end'

        Button:
            name: 'btn2'
            id: btn2
            text: "btn2"
            on_release:
                startvalue = 2
                app.root.current = 'end'

        Button:
            name: 'btn3'
            id: btn3
            text: "btn3"
            on_release:
                startvalue = 3
                app.root.current = 'end'

        Button:
            name: 'btn4'
            id: btn4
            text: "btn4"
            on_release:
                startvalue = 4
                app.root.current = 'end'


<End>:
    id: end
    name: "end"
    endbtn: endbtn
    bg_image: bg_image

    on_pre_enter: self.which_color()

    Image:
        id: bg_image
        source: ""
        allow_stretch: True

    Button:
        name: 'endbtn'
        id: endbtn
        text: "end"
        size_hint_y: .35
        size_hint_x: .25
        on_release: app.root.current = 'start'

我意识到全局变量非常危险,我宁愿不使用它们,但我不知道它是如何工作的。另外,我想知道如何在Kivy中更改Python全局变量,然后在第二个类中使用它们以确定屏幕背景。

2 个答案:

答案 0 :(得分:1)

解决方案是使用Kivy NumericProperty并将其连接起来。有关详细信息,请参阅以下示例。

实施例

main.py

script:
    - ./script.sh

test.kv

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import NumericProperty


class ScreenManagement(ScreenManager):
    pass


class Start(Screen):
    startvalue = NumericProperty(0)


class End(Screen):

    def which_color(self, startvalue):
        print("startvalue={}".format(startvalue))
        if startvalue == 1:
            self.bg_image.source = 'blue.png'
        elif startvalue == 2:
            self.bg_image.source = 'red.png'
        elif startvalue == 3:
            self.bg_image.source = 'yellow.jpg'
        elif startvalue == 4:
            self.bg_image.source = 'green.jpg'


class TestApp(App):
    def build(self):
        return ScreenManagement()


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

输出

Button #4 Pressed - Green Image Displayed

答案 1 :(得分:0)

您尝试使用第一个屏幕中的值:

<End>:
    id: end
    name: "end"
    endbtn: endbtn
    bg_image: bg_image

    on_pre_enter: 
        self.which_color(self.manager.get_screen('start').startvalue)

在python文件中:

def which_color(self, startvalue):
    ...