在 Kivy 中将自定义方法分配给 on_touch_down 等
Assigning custom method to on_touch_down etc. in Kivy
我正在使用 Kivy 编写最终成为移动游戏应用程序的内容。考虑到框架的功能——能够分离形式和功能——我正在尝试使用 Kivy 语言在 .kv 文件中完成我的 GUI 的大部分(如果不是全部)设计。就制作布局而言,这非常有效,但事实证明,让触摸事件处理程序正常工作非常具有挑战性。我正在尝试做的是:
Python:
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,但是 documentation for on_touch_down 表明它接收到一个 touch
参数。
args
关键字在on_回调中可用docs also mention。
将这两个放在一起,您应该能够通过以下方式将触摸参数传递给 python:
on_touch_down: root.pc_move(args[1])
[我不确定它将成为 args[]
中的 #1 元素,但一些示例似乎表明]
我正在使用 Kivy 编写最终成为移动游戏应用程序的内容。考虑到框架的功能——能够分离形式和功能——我正在尝试使用 Kivy 语言在 .kv 文件中完成我的 GUI 的大部分(如果不是全部)设计。就制作布局而言,这非常有效,但事实证明,让触摸事件处理程序正常工作非常具有挑战性。我正在尝试做的是:
Python:
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,但是 documentation for on_touch_down 表明它接收到一个 touch
参数。
args
关键字在on_回调中可用docs also mention。
将这两个放在一起,您应该能够通过以下方式将触摸参数传递给 python:
on_touch_down: root.pc_move(args[1])
[我不确定它将成为 args[]
中的 #1 元素,但一些示例似乎表明]