0
kivyライブラリを初めて使用し、プロパティを動的に更新する際に問題が発生しました。ここのラベルは単なるプレースホルダです。最終的には、ユーザーがクリック/タッチする象限に基づいて、表示されるイメージを順番に変更したいと思っています。kivyプログラム(Python)のラベルテキストを動的に更新
プログラムは正常に実行され、エラーは表示されず、ラベル(label2)は更新されません(label1は更新されます)。 4つの象限をクリックすると、象限番号が私が期待するようにコンソールに表示されます。ユーザーがQ1をクリックするたびにself.incrを表示しています。これも表示され、増加します。これは、incr属性が増加していることを意味します。
だから、なぜそれがラベルのために更新されないのか分かりません。
main.py
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.image import Image
class TouchInput(Widget):
def __init__(self,**kwargs):
self.incr = 5
super(TouchInput,self).__init__(**kwargs)
def on_touch_up(self, touch):
if touch.x < self.width/2:
lateral = 'left'
elif touch.x > self.width/2:
lateral = 'right'
else:
lateral = None
if touch.y < self.height/2:
vertical = 'bottom'
elif touch.y > self.height/2:
vertical = 'top'
else:
vertical = None
if vertical and lateral:
if lateral == 'left' and vertical == 'top':
quadrant = 1
print 'Q1'
self.incr += 1
print self.incr
elif lateral == 'right' and vertical == 'top':
quadrant = 2
print 'Q2'
elif lateral == 'left' and vertical == 'bottom':
quadrant = 3
print 'Q3'
elif lateral == 'right' and vertical == 'bottom':
quadrant = 4
print 'Q4'
class PPVT(App):
def build(self):
t = TouchInput()
print t.incr
return t
if __name__ == "__main__":
PPVT().run()
main.kv
<TouchInput>:
Image:
source: 'img1.jpg'
size: root.width, root.height
Label:
id: label1
text: str(root.width)
pos: root.width/2, root.height/2
Label:
id: label2
text: str(root.incr)
ありがとうございます。私はなぜself.attrで属性を設定できず、self.widthのようなものを追跡しているときにkivyが追跡しているのか不思議です。 – Daniel
@Daniel 'self.width'は、おそらく数値プロパティでもあるプロパティです。 – jligeza