如何在kivy中从python脚本访问kv中的属性

时间:2017-04-08 03:44:32

标签: python kivy

以下是main.py:

from kivy.app import App
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.screenmanager import (ScreenManager, Screen)
from kivy.uix.button import Button
from kivy.properties import *

class EmotionsScreen(Screen):
    def button_press(self, emotion_str):
        print (self.ids[emotion_str].emotion)

class ScreenManagement(ScreenManager):
    pass

class HappyButton(ButtonBehavior, Image):
    def on_press(self):
        print(self.emotion)

class SadButton(ButtonBehavior, Image):
    def on_press(self):
        print(self.emotion)

class TiredButton(ButtonBehavior, Image):
    def on_press(self):
        print(self.emotion)

class MyApp(App):
    def build(self):
        sm = ScreenManagement()
        sm.current = 'Emotions'
        return sm

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

以下是myapp.kv文件的内容:     :         EmotionsScreen:

<EmotionsScreen>:
    name:'Emotions'

    HappyButton:
        id: happy
        source: "happy.png"
        pos: (-200, 100)
        emotion: "Happy"
    SadButton:
        id: sad
        source: "sad.png"
        pos: (0, 100)
        emotion: "Sad"
    TiredButton:
        id: tired
        source: "tired.png"
        pos: (200, 100)
        emotion: "Tired"

运行应用程序并单击三个按钮后,我收到以下行为:

  • 点击快乐按钮打印“悲伤”:)
  • 单击悲伤按钮会打印“累了”
  • 点击累了按钮会打印“累了”

即使我调用单个回调(在应用内部,提供参数),上述情况也会发生,如下所示:

main.py:

from kivy.app import App
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.screenmanager import (ScreenManager, Screen)
from kivy.uix.button import Button
from kivy.properties import *

class EmotionsScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

class ImageButton(ButtonBehavior, Image):
    pass

class MyApp(App):
    def build(self):
        sm = ScreenManagement()
        sm.current = 'Emotions'
        return sm

    def button_press(self, *args):
        print (args)


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

myapp.kv:

<ScreenManagement>:
    EmotionsScreen:

<EmotionsScreen>:
    name:'Emotions'

    ImageButton:
        source: "happy.png"
        pos: (-200, 100)
        on_press: app.button_press("Happy")
    ImageButton:
        source: "sad.png"
        pos: (0, 100)
        on_press: app.button_press("Sad")
    ImageButton:
        source: "tired.png"
        pos: (200, 100)
        on_press: app.button_press("Tired")

我希望它分别点击三个按钮时打印,“快乐”,“悲伤”和“累”。我可以知道我哪里出错吗?

1 个答案:

答案 0 :(得分:1)

这些图像似乎是随机放置的。使用按钮作为模型的示例:

from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.screenmanager import (ScreenManager, Screen)
from kivy.uix.button import Button
from kivy.properties import *
from kivy.lang import Builder

Builder.load_string('''
<EmotionsScreen>:
    name:'Emotions'
    HappyButton:
        id: happy
        pos: (-200, 100)
        text: "Happy"
        emotion: "Happy"
    SadButton:
        id: sad
        pos: (0, 100)
        text: "Sad"
        emotion: "Sad"
    TiredButton:
        id: tired
        pos: (200, 100)
        text: "Tired"
        emotion: "Tired"
''')

class EmotionsScreen(Screen):
    def button_press(self, emotion_str):
        print (self.ids[emotion_str].emotion)

class ScreenManagement(ScreenManager):
    pass

class HappyButton(Button):
    def on_press(self):
        print(self.emotion)

class SadButton(Button):
    def on_press(self):
        print(self.emotion)

class TiredButton(Button):
    def on_press(self):
        print(self.emotion)

class MyApp(App):
    def build(self):
        sm = ScreenManagement()
        sm.add_widget(EmotionsScreen())
        sm.current = 'Emotions'
        return sm

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

您应该在屏幕内放置一个布局来管理其小部件:

Builder.load_string('''
<EmotionsScreen>:
    name:'Emotions'

    BoxLayout:
        HappyButton:
            id: happy
            text: "Happy"
            emotion: "Happy"
        SadButton:
            id: sad
            text: "Sad"
            emotion: "Sad"
        TiredButton:
            id: tired
            text: "Tired"
            emotion: "Tired"
''')