[英]Assigning custom method to on_touch_down etc. in Kivy
我正在使用Kivy编写最终成为移动游戏应用程序的内容。 鉴于框架的功能 - 能够分离表单和功能 - 我试图使用Kivy语言在.kv文件中完成大部分(如果不是全部)GUI设计。 这对于制作布局非常有用,但让触摸事件处理程序正常工作证明非常具有挑战性。 我试图做的是:
蟒蛇:
from kivy import require
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.boxlayout import BoxLayout
require("1.9.1")
class gameScreen(FloatLayout):
def move_scatter(self, sender):
self.ids.moveArea.x = sender.text
def pc_move(self, touch, *args, **kwargs):
print('Goodbye')
# self.ids.protagonist.pos = (self.x + )
class GameApp(App):
def __init__(self, **kwargs):
super(GameApp, self).__init__(**kwargs)
def build(self):
return gameScreen()
class MoveBox(BoxLayout):
pass
class Pc(Image):
pass
if __name__ == '__main__':
GameApp().run()
Kivy代码:
<gameScreen>:
orientation: 'vertical'
padding: 20
canvas.before:
Rectangle:
size: (root.width, root.height)
source: 'bliss.jpg'
Pc:
id: protagonist
TextInput:
id: debugOut
size_hint: None, None
size: 200, 50
text: 'Hello'
BoxLayout:
id: moveArea
size_hint: None, None
size: 200, 200
on_touch_down: root.pc_move()
canvas:
Color:
rgba: .2, .2, .2, .5
Rectangle:
pos: (self.x + root.width - self.width, self.y)
size: self.size
<Pc>
source:'voolf.png'
pos_hint: {'top': .9}
size_hint: None, None
size: 300, 300
当我尝试这个时,我得到:
TypeError: pc_move() takes at least 2 arguments (1 given)
这显然是有道理的,因为我在不传递参数的情况下调用pc_move()方法。 我知道解决这个问题的最简单方法就是在我的Python代码中创建BoxLayout并在那里定义on_touch_down方法,但正如所述,我试图将我的GUI和功能分开。
问题是,如果我要在Python代码中创建窗口小部件,如何通过'touch'参数传递? 或者,我只是在追逐白鲸吗? 事件处理是否必须在Python代码中创建的窗口小部件中完成?
我承认我从未使用过Kivy,但是on_touch_down的文档表明它接收到了一个touch
参数。
该文档还提到说args
关键字在ON_回调可用 。
把这两个放在一起,你应该能够通过以下方式将你的touch参数传递给python:
on_touch_down: root.pc_move(args[1])
[我不肯定它将成为args[]
的#1元素,但有些例子似乎表明]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.