1299浏览
查看: 1299|回复: 2

[教程] 通过手势识别上下左右滑动

[复制链接]
先看看效果!


直接上代码
不清楚的留言交流
  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)
复制代码


Forgotten  版主

发表于 2023-3-1 11:17:41

回复

使用道具 举报

ASH腻  管理员

发表于 2023-3-6 19:12:10

wow 效果不错
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail