gpt4 book ai didi

python - 使用 Kivy 正确创建鼠标事件

转载 作者:太空宇宙 更新时间:2023-11-03 16:18:41 27 4
gpt4 key购买 nike

我正在尝试创建一个程序来控制我的 Kivy 应用程序上的鼠标。创建提供程序并向其发送我想要移动和单击的位置的正确方法是什么?

最佳答案

看一下记录器模块,它既可以记录事件,也可以回放它们

这是一个小例子:(将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?

关于python - 使用 Kivy 正确创建鼠标事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38685626/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com