我正在尝试创建一个程序来控制我的Kivy应用程序上的鼠标。创建提供程序并将其发送到我想要移动的位置并单击的位置的正确方法是什么?
答案 0 :(得分:1)
看看录音机模块,它既可以录制事件也可以重放它们
这是一个小例子:(将RECORD改为False,以便在录制后观看重播......)
import kivy
from kivy.uix.button import Button
from kivy.app import App
from kivy.input.recorder import Recorder
rec = Recorder(filename='myrecorder.kvi',
record_attrs=['is_touch', 'sx', 'sy', 'angle', 'pressure'],
record_profile_mask=['pos', 'angle', 'pressure'])
def funky(b):
print("Hello!!!")
if RECORD:
rec.record = False
else:
rec.play = False
exit(0)
class MyApp(App):
def build(self):
if RECORD:
rec.record = True
else:
rec.play = True
return Button(text="hello", on_release=funky)
if __name__ == '__main__':
RECORD = True # False for replay
MyApp().run()
现在你可以看到文件myrecorder.kvi:
#RECORDER1.0
(1.1087048053741455, 'begin', 1, {'profile': ['pos'], 'sx': 0.65875, 'is_touch': True, 'sy': 0.51})
(1.1346497535705566, 'update', 1, {'profile': ['pos'], 'sx': 0.66, 'is_touch': True, 'sy': 0.51})
(1.1994667053222656, 'end', 1, {'profile': ['pos'], 'sx': 0.66, 'is_touch': True, 'sy': 0.51})
您可以通过许多其他方式使用Recorder类,请参阅文档: https://kivy.org/docs/api-kivy.input.recorder.html
您可以将录音机包装在一个函数中以制作一个小助手:
#not tested
def click(x, y):
with open("clicker.kvi", 'w') as f:
f.write("""\#RECORDER1.0
(0.1087048053741455, 'begin', 1, {{'profile': ['pos'], 'sx': {x}, 'is_touch': True, 'sy': {y}}})
(0.1346497535705566, 'update', 1, {{'profile': ['pos'], 'sx': {x}, 'is_touch': True, 'sy': {y}}})
(0.1994667053222656, 'end', 1, {{'profile': ['pos'], 'sx': {x}, 'is_touch': True, 'sy': {y}}})""".format(x=x, y=y))
rec = Recorder(filename='clicker.kvi',
record_attrs=['is_touch', 'sx', 'sy', 'angle', 'pressure'],
record_profile_mask=['pos', 'angle', 'pressure'])
rec.play = True
#should call rec.play = False somewhere?