2016-03-27 11 views
2

"scripts/events.py"のEventクラスの変数 "current_text"を更新するには、 "EventWindow"のラベルのテキストを取得しようとしています。答えは "current_text"を "ct"にバインドしているかもしれませんが、 "AttributeError:class Eventには属性 'bind'がありません。私がこの解決法で間違った木を吠えているなら、私は別の方法をとても受け入れています。以下は Kivy - ラベルのテキストを別のクラスの変数にバインドする

は、関連するコードスニペットですが、完全なプロジェクトは次の場所にあります。 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: 
     ... 

ありがとう!

+0

'Event'には、kivyで使用されるイベントを処理するための実装されたメソッドとメカニズムがありません。 'Player'クラスがちょうどうまくいくべきことをしない限り、それを[' EventDispatcher'](https://kivy.org/docs/api-kivy.event.html)のサブクラスにしてみてください。 – zeeMonkeez

+0

これは完全に機能し、絶対的に意味があります。あなたがそれを再提出したい場合は、これを正解として喜んで登録します。 – DinkWerks

答えて

0

コードでは、EventPlayerから得られ、それ自体はold style classです。重要なことには、どちらもイベント管理に必要なメソッドを実装していません(例えば、fbind;インタフェースを定義するスタブクラスの場合はObservableを参照)。ほとんどの場合、EventまたはPlayerのサブクラスをEventDispatcherのサブクラスにして、イベントkivy-styleを作成してバインドするだけで十分です。

関連する問題