我的目標是觀察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()
這兩個答案都很完美。 – xxLITxx