Kivy将标签文本绑定到另一个类中的变量

2024-10-01 02:26:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在“脚本”事件类中的变量“current_text”时更新“EventWindow”中标签的文本/事件.py”. 我知道答案可能在于将“current”绑定到“ct”,但这只会导致“AttributeError:class Event没有属性‘bind’”。如果我用这个方法找错了方向,我会很乐意接受另一种方法。在

下面是相关的代码片段,但是完整的项目可以在:https://github.com/DinkWerks

在主.py在

from kivy.properties import ObjectProperty, StringProperty
from kivy.app import App
# Utility Imports
import yaml
# Game Imports
from scripts.events import Event

# File Loads
loc_file = file('data/location.yml')
loc_dat = yaml.load(loc_file)

# Bind Classes
event = Event()

class EventWindow(BoxLayout):
    ct = StringProperty('')

    def __init__(self, **kwargs):
        super(EventWindow, self).__init__(**kwargs)
        self.ct = event.current_text
        # Error occurs below. Comment out too see semi-functional app.
        Event.bind(current_text=self.setter('ct'))

脚本/事件.py在

^{pr2}$

在文本.kv在

<EventWindow>:
    BoxLayout:
        pos: 100,100
        size_hint: .4,.7
        orientation: 'vertical'
        Image:
            source: 'maps/map.jpg'
            pos: self.pos
            size: self.size
        ScrollView:
            canvas.before:
                Color:
                    rgba: [.2,.2,.2,.8]
                Rectangle:
                    size: self.size
                    pos: self.pos
            Label:
                id: text_area
                text: root.ct
                padding: 15,10
                text_size: self.width, None
                size_hint_y: None
                height: self.texture_size[1]
<Foo>:
    id: bl
    popup: popup.__self__
    header: header
    BoxLayout:
        orientation: 'vertical'
        BoxLayout:
            ...
        FloatLayout:
            id: mapspace
            canvas:
                ...
            EventWindow:
                id: event
    Popup:
        ...

谢谢!在


Tags: textfrompyposimportselfeventid
1条回答
网友
1楼 · 发布于 2024-10-01 02:26:22

在您的代码中,Event是从Player派生的,它本身就是一个old style class。重要的是,它们都没有实现事件管理所需的方法(例如,fbind;对于定义接口的存根类,请参见^{})。很可能只要使EventPlayer成为{a3}的子类,就可以创建并绑定到事件kivy样式。在

相关问题 更多 >