python - 如何在 python kivy 应用程序中左右滑动?

标签 python kivy

我想在我的 kivy 应用程序中添加一个滑动事件,据我所知,kivy 没有像 on_touch_left 这样的事件。或 on_touch_right可用,但它还有另一个 on_touch_move我认为可以用于此目的的功能

class TestWidget(BoxLayout):
    def on_touch_move(self, touch):
        print touch.x

我在上面的代码中注意到,如果我们向右滑动 touch.x值增加,如果我们向右滑动 touch.x值(value)下降。我们只需要取第一个和最后一个之间的差异touch.x预测左/右滑动的值。

问题是如何存储和检索touch.x从初始值到最终值。

最佳答案

除了使用 on_touch_move 事件,您可以使用 on_touch_down 并保存 touch.x 然后使用 on_touch_up 和比较touch.x,例如:

initial = 0
def on_touch_down(self, touch):
    initial = touch.x

def on_touch_up(self, touch):
    if touch.x > initial:
        # do something
    elif touch.x < initial:
        # do other thing
    else: 
        # what happens if there is no move

更好的方法是比较使用 if touch.x - initial > some-value 设置最小滑动范围来执行某些操作。

关于python - 如何在 python kivy 应用程序中左右滑动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45144228/

相关文章:

python - 文本文件之间的交集

python - 如何在 Kivy ScrollView 中滚动 GridLayout?

python - os.startfile() 未打开 .docx 文件

python - 如果 Pandas Series 使用 numpy,我如何在 Pandas Series 中拥有不同的类型?

python - 将 Dash 与套接字 API 一起使用

python - 如何生成计数器的所有子集?

global-variables - kivy:全局变量和ScreenManager

opencv - Android SDK 工具 : OpenCV requires Android SDK Tools revision 14 or newer

python - Kivy: 'NoneType' 对象不可调用,按钮删除时

python - 从另一个类中的 InputText 获取值。基维