![](/img/trans.png)
[英]How to update the on_touch_move and on_touch_down methods of a Widget in kivy
[英]Kivy: How to change size of widget using on_touch_move function?
作为初学者 python 学习者,我正在尝试使用 kivy 创建这个简单的应用程序,以通过各种输入来更改矩形的粗细。 首先,我尝试使用按钮来完成它,并在这个社区的一些帮助下设法使它工作。
现在这个问题已经解决了,我想通过使用on_touch_move
function 在屏幕上滑动来改变厚度来将它提升到一个新的水平。 但又一次偶然发现了一个新问题。
当我运行此代码时,没有错误, boundary_thickness_x
和boundary_thickness_y
也正在更新(使用打印测试)。 但是小部件的大小(厚度)在 window 中没有更新。
我想知道我可能会犯什么错误?
**main.py**
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ObjectProperty
class Boundary(Widget):
boundary_thickness_x = NumericProperty(10)
boundary_thickness_y = NumericProperty(10)
def on_touch_move(self, touch):
x = touch.x/self.width * 100
y = touch.y/self.height * 100
boundary_thickness_x = x
boundary_thickness_y = y
#print(boundary_thickness_x, boundary_thickness_y)
class BounceApp(App):
def build(self):
return Boundary()
BounceApp().run()
**bounce.kv**
<Boundary>
canvas:
Rectangle:
pos : 0, 0
size: self.boundary_thickness_x, root.height
Rectangle:
pos : 0, 0
size: root.width, self.boundary_thickness_y
Rectangle:
pos : root.width - self.boundary_thickness_x, 0
size: self.boundary_thickness_x, root.height
Rectangle:
pos : 0, root.height - self.boundary_thickness_y
size: root.width, self.boundary_thickness_y
您的on_touch_move()
方法没有调整正确的属性。 它只是调整几个局部变量。 只需将该方法更改为:
def on_touch_move(self, touch):
x = touch.x / self.width * 100
y = touch.y / self.height * 100
self.boundary_thickness_x = x
self.boundary_thickness_y = y
你必须使用self.
引用属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.