0
新的kivy库和动态更新属性有一些麻烦。这里的标签只是一个地方持有人。最终,我希望显示的图像根据用户点击/触摸哪个象限顺序更改。动态更新kivy程序(Python)中的标签文本
程序运行正常,没有错误,悬停标签(label2)不更新(label1更新)。当我点击四象限时,象我期望的那样,象限号码显示在控制台上。我也打印出self.incr,无论用户什么时候点击Q1,这也会显示并增加,这意味着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