通过手势识别上下左右滑动

2023-02-287752
AI 快速预览详细 收起
本文介绍了如何使用行空板K10实现手势识别上下左右滑动。通过导入unihiker库并实例化GUI类,结合鼠标事件来判断手势方向,并根据不同的手势切换图片显示。这个项目适合零基础新手学习,可以应用于STEM教育场景中,帮助孩子体验科技的乐趣。
先看看效果!


直接上代码
不清楚的留言交流
  1. import time
  2. from unihiker import GUI  # 导入包
  3. gui = GUI()  # 实例化GUI类
  4. img_image2 = gui.draw_image(x=0, y=0, image='4.png')
  5. def mouse_move(x, y):
  6.     print("坐标:x={},y={}".format(x, y))
  7.     if compare(x, 90, 170) and compare(y, 80, 110):
  8.         img_image2.config(image="1.png")
  9.     elif compare(x, 35, 70) and compare(y, 110, 185):
  10.         img_image2.config(image="2.png")
  11.     elif compare(x, 120, 230) and compare(y, 200, 240):
  12.         img_image2.config(image="4.png")
  13.     elif compare(x, 169, 210) and compare(y, 110, 185):
  14.         img_image2.config(image="3.png")
  15. def compare(val, min, max):
  16.     if val in range(min, max + 1):
  17.         return True
  18.     else:
  19.         return False
  20. xx = 0
  21. yy = 0
  22. def press(x):
  23.     global xx
  24.     xx = [x.x, x.y]
  25.     # print(xx)
  26. def release(y):
  27.     yy = [y.x, y.y]
  28.     temp = yy[0] - xx[0]
  29.     temp1 = yy[1] - xx[1]
  30.     if temp > 30:
  31.         # print("右滑")
  32.         img_image2.config(image="3.png")
  33.     if temp < -30:
  34.         # print("左滑")
  35.         img_image2.config(image="2.png")
  36.     if temp1 > 30:
  37.         # print("下滑")
  38.         img_image2.config(image="4.png")
  39.     if temp1 < -30:
  40.         # print("上滑")
  41.         img_image2.config(image="1.png")
  42. #     print(temp1)
  43. # 通过点击方式切换按钮
  44. # gui.on_mouse_move(mouse_move)
  45. # 通过手势切换按钮
  46. gui.master.bind('<Button-1>', press)  # 鼠标左键按下
  47. gui.master.bind("<ButtonRelease>", release)  # 鼠标左键释放
  48. while True:
  49.     # 增加等待,防止程序退出和卡住
  50.     time.sleep(1)
复制代码


创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(2)
Ash1
Ash1
wow 效果不错
Forgotten
Forgotten
评论内容
- 没有更多了 -