Swipe up in python

How to create a swipe up function in python tkinter

Since you didn’t give any more details about this is jus a general one:

import tkinter as tk

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        # remember that first you have to create a canvas to detect swipe up
        self.canvas = tk.Canvas(self, width=400, height=400)
        self.canvas.pack()

        # after that you create the binds. Bind the mouse button down, motion, and button up events
        self.canvas.bind("<Button-1>", self.mouse_down)
        self.canvas.bind("<B1-Motion>", self.mouse_move)
        self.canvas.bind("<ButtonRelease-1>", self.mouse_up)

        # initial position of the mouse
        self.start_y = None

    def mouse_down(self, event):
        # save the initial position of the mouse
        self.start_y = event.y

    def mouse_move(self, event):
        pass  # we don't need to do anything on mouse move

    def mouse_up(self, event):
        # check if the mouse moved up
        if self.start_y is not None and event.y < self.start_y:
            print("Swipe up detected.")

if __name__ == "__main__":
    app = App()
    app.mainloop()

This is just a simple one to give you a start you may change it later depending on what you need.

4 Likes