2017-05-09 13 views
1

私の目標は、Popupの番号の数を見ることです。 NumericPropertyがロードされています。ただし、コールバックが呼び出されたときの数値は変更されません。 (私はlabel.textにコールバックするコードはありません)Kivyアップデートダイナミックラベルテキスト

同様の質問があります。しかし、私は彼らがこの特定の場合にどのように適用されるか見ることができませんでした。 Similar Case

import kivy 
kivy.require("1.7.0") 

from kivy.app import App 
from kivy.uix.popup import Popup 
from kivy.uix.label import Label 
from kivy.uix.button import Button 
from kivy.uix.boxlayout import BoxLayout 
from kivy.properties import ObjectProperty 
from kivy.properties import NumericProperty 
from kivy.clock import Clock 
from kivy.event import EventDispatcher 

scoreInc = 0 

class MyPopup(Popup): 

    def show_popup(self): 

     content = BoxLayout(orientation="vertical") 

     self.incrementerFnc = Clock.schedule_interval(self.incrementer, .005) 

     scoreLabel = Label(text=str(ins.a), id='scorelabel', font_size=20) 

     content.add_widget(scoreLabel) 

     mybutton = Button(text="Close", size_hint=(1,.20), font_size=20) 
     content.add_widget(mybutton) 

     mypopup = Popup(content = content,    
       title = "Score",  
       auto_dismiss = False,   
       size_hint = (.7, .5),   
       font_size = 20) 
     mybutton.bind(on_press=mypopup.dismiss) 
     mypopup.open() 

    def incrementer(self, dt): 
     global scoreInc 
     scoreInc += 1 

     ins.a = scoreInc 

     if(scoreInc >= 10): 
      Clock.unschedule(self.incrementerFnc) 
      print('quit') 
     else: 
      print('scoreInc', ins.a)  

class MyClass(EventDispatcher): 
    a = NumericProperty(0) 

def callback(instance, value): 
    print('My callback is call from', instance) 
    print('and the a value changed to', value) 

ins = MyClass() 
ins.bind(a=callback) 


class MyApp(App):  

    def build(self): 

     mypopup = MyPopup() 

     return mypopup.show_popup() 

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

答えて

2

あなたはあなたのMyClassで処理する必要があなたのscoreLabelのテキスト値を更新したイベントが欠落している、以下を参照:プロパティa更新は、on_aがトリガされ

class MyClass(EventDispatcher): 
    a = NumericProperty(0) 
    def on_a(self, instance, value): 
     app = App.get_running_app() 
     app.scoreLabel.text = str(value) 

を使用して scoreLabelの値を更新することができます。それ以外の場合は接続されていません。 text = str(ins.a)行は、 ins.aから の値がで、それを 0としています。

しかし、何とかして、scoreLabelにアクセスする必要があります。

app = App.get_running_app() 
    app.scoreLabel = Label(text=str(ins.a), id='scorelabel', font_size=20) 

    content.add_widget(app.scoreLabel) 

この方法で、あなたも後でon_aイベントでそれにアクセスすることができます:あなたは後で使用するためのインスタンスを保存することができApp.get_running_app()。または、selfを使用してApp.get_running_app().popupで直接Popupにアクセスし、その内容を入力します。

App.get_running_app()しかし、時には好ましいオプションではない可能性があります。したがって、グローバルを使用することもできますし、他の方法でインスタンスを保存することもできます。他のクラスの中に。あなたはPopupを持っている場合は、そのウィジェットは、例えばルートWindowに自分自身を追加し、それが中に保存されています:

Window -> children -> Popup -> content -> layout -> scoreLabel 

が、直接ウィンドウをいじっては不幸な結果を持っている可能性があるので、それに注意してください。

2

これで私は別のアプローチを採用します。
グローバルを使用する代わりに、カスタムレイアウトでスコアをプロパティとして使用し、その値をポップアップに渡します。

from kivy.app import App 
from kivy.lang import Builder 
from kivy.uix.popup import Popup 
from kivy.uix.boxlayout import BoxLayout 
from kivy.clock import Clock 


KV = ''' 

<MyPopup>: 
    title: "Game over" 
    score_label: score_label 
    id: popup 
    content: bl 
    BoxLayout: 
     id: bl 
     Label: 
      id: score_label 
      text:"Your score is" 
      font_size:20 
     Button: 
      text:"Close this!!" 
      on_release: popup.dismiss() 


MyLayout: 

    orientation: "vertical" 
    Label: 
     text: "Type in score" 
    TextInput: 
     id: score_inp 
    Button: 
     text: "Open gameover popup" 
     on_release: 
      root.gameover_popup.open() 
      root.gameover_popup.gameover(score_inp.text) 

''' 


class MyPopup(Popup): 

    def gameover(self,score): 
     self.iterations = int(score) 
     self.score = 0 
     self.event = Clock.schedule_interval(self.set_label,0.1) 

    def set_label(self,dt): 
     self.score += 1 
     self.score_label.text = str(self.score) 
     if self.score >= self.iterations: 
      self.event.cancel() 



class MyLayout(BoxLayout): 

    def __init__(self,**kwargs): 
     super(MyLayout,self).__init__(**kwargs) 
     self.gameover_popup = MyPopup() 



class MyApp(App): 

    def build(self): 
     return Builder.load_string(KV) 


MyApp().run() 
+0

両方の回答が完全に機能します。 – xxLITxx