Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从kivy中的python脚本访问kv中的属性_Python_Kivy - Fatal编程技术网

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

如何从kivy中的python脚本访问kv中的属性,python,kivy,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 Emo

以下是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>:
    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"
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")
:
情绪筛选:
:
姓名:'情感'
图像按钮:
资料来源:“happy.png”
位置:(-200100)
按下:应用按钮按下(“快乐”)
图像按钮:
资料来源:“sad.png”
位置:(01100)
按下:应用按钮按下(“Sad”)
图像按钮:
资料来源:“tired.png”
职位:(200100)
按下:应用按钮按下(“疲劳”)

我希望在分别点击这三个按钮后,它会打印出“高兴”、“悲伤”和“疲惫”。我可以知道我哪里出错了吗?

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

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()
从kivy.app导入应用
从kivy.uix.image导入图像
从kivy.uix.screenmanager导入(screenmanager,Screen)
从kivy.uix.button导入按钮
从kivy.properties导入*
从kivy.lang导入生成器
Builder.load_字符串(“”)
:
姓名:'情感'
快乐按钮:
id:高兴吗
位置:(-200100)
文字:“快乐”
情感:“快乐”
Sad按钮:
id:悲哀
位置:(01100)
文字:“悲伤”
情绪:“悲伤”
轮胎按钮:
id:累了吗
职位:(200100)
文字:“累了”
情绪:“累了”
''')
类情绪屏幕(屏幕):
def按钮按下(自我、情绪):
打印(self.ids[emotion\u str].emotion)
类屏幕管理(屏幕管理器):
通过
班级快乐按钮(按钮):
def on_按下(自):
印刷(自我情感)
类按钮(按钮):
def on_按下(自):
印刷(自我情感)
类轮胎按钮(按钮):
def on_按下(自):
印刷(自我情感)
类别MyApp(应用程序):
def生成(自):
sm=屏幕管理()
sm.add_小部件(EmotionsScreen())
sm.current=‘情感’
返回sm
如果“名称”=“\uuuuuuuu主要”:
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"
''')
Builder.load\u字符串(“”)
:
姓名:'情感'
盒子布局:
快乐按钮:
id:高兴吗
文字:“快乐”
情感:“快乐”
Sad按钮:
id:悲哀
文字:“悲伤”
情绪:“悲伤”
轮胎按钮:
id:累了吗
文字:“累了”
情绪:“累了”
''')

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

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()
从kivy.app导入应用
从kivy.uix.image导入图像
从kivy.uix.screenmanager导入(screenmanager,Screen)
从kivy.uix.button导入按钮
从kivy.properties导入*
从kivy.lang导入生成器
Builder.load_字符串(“”)
:
姓名:'情感'
快乐按钮:
id:高兴吗
位置:(-200100)
文字:“快乐”
情感:“快乐”
Sad按钮:
id:悲哀
位置:(01100)
文字:“悲伤”
情绪:“悲伤”
轮胎按钮:
id:累了吗
职位:(200100)
文字:“累了”
情绪:“累了”
''')
类情绪屏幕(屏幕):
def按钮按下(自我、情绪):
打印(self.ids[emotion\u str].emotion)
类屏幕管理(屏幕管理器):
通过
班级快乐按钮(按钮):
def on_按下(自):
印刷(自我情感)
类按钮(按钮):
def on_按下(自):
印刷(自我情感)
类轮胎按钮(按钮):
def on_按下(自):
印刷(自我情感)
类别MyApp(应用程序):
def生成(自):
sm=屏幕管理()
sm.add_小部件(EmotionsScreen())
sm.current=‘情感’
返回sm
如果“名称”=“\uuuuuuuu主要”:
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"
''')
Builder.load\u字符串(“”)
:
姓名:'情感'
盒子布局:
快乐按钮:
id:高兴吗
文字:“快乐”
情感:“快乐”
Sad按钮:
id:悲哀
文字:“悲伤”
情绪:“悲伤”
轮胎按钮:
id:累了吗
文字:“累了”
情绪:“累了”
''')

非常感谢。成功了!我的分数不到15分,所以我不能把你的答案投得太高。成功了!我的分数不到15分,所以我不能投票支持你的答案