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

时间:2016-03-27 06:30:16

标签: python python-2.7 user-interface kivy

我试图让“EventWindow”中的标签文本在“scripts / events.py”中的Event类中变量“current_text”时更新。我意识到答案可能在于将“current_text”绑定到“ct”,但这只会导致“AttributeError:class Event没有属性'bind'”。如果我用这个解决方案咆哮错误的树,我会非常接受另一种方法。

以下是相关的代码段,但完整项目位于:https://github.com/DinkWerks

main.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'))

脚本/ events.py

from kivy.properties import StringProperty
...
from player import Player
from enemy import Enemy


class Event(Player):
    open_events = file('data/event.yml', 'r')
    event_file = yaml.load(open_events)
    current_text = StringProperty('1234')

    def __init__(self):
        Player.__init__(self)
        self.events = Event.event_file
        self.selection = ''
        self.current_text = '1234'

def event_name(self):
    ...

def event_selector(self, eid):
    ...

def parse(self):
    driver = 1
    variables = ('Name', 'Is_Person', 'Level', 'Gold')
    poss_commands = ("[Next Slide]", "[Query]", "[Terminate]", "[Combat]")

    while driver >= 0:
        text = self.events[self.selection][driver]
        lexer = shlex(text)
        lexer.quotes = '/'

        output = ''
        command = ''
        for token in lexer:
            if token in variables:
                output += str(eval('Player.' + token))
            elif token.replace('/', '') in poss_commands:
                command += token.replace('/', '')
            else:
                output += token.replace('/', '')
        self.current_text = output
        driver += self.controller(command)
    self.modifier()

def modifier(self):
    ...

def controller(self, cmd):
    ...

text.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:
        ...

谢谢!

1 个答案:

答案 0 :(得分:0)

在您的代码中,Event源自Playerfbind本身就是old style class。重要的是,既不实现事件管理所需的方法(例如Event;对于定义接口的存根类,请参见Observable)。最有可能的是,使Player> stack build > stack exec -- mysite 成为EventDispatcher的子类,以便能够创建并绑定到kivy风格的事件。