Python Forum
tkinter touchscreen scrolling - button press makes unwanted scrolling
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter touchscreen scrolling - button press makes unwanted scrolling
#1
Hi all,

I have a python tkinter GUI that I am using with a touchscreen. One page on my GUI has more buttons than can fit in one screen, so I found some excellent code that creates a vertical scrolled frame. With this frame I can drag my finger on the touchscreen and the frame will move accordingly.

My issue is when I press a button, the scrolling happens too. I basically have to "chase the button" up or down the screen (depending on if the button is located in the top or bottom half of the screen).

I wish this was easily reproduceable but this issue only occurs when I am using my finger on the touchscreen, it does not happen when I am using the mouse. I tried using a stylus pen (maybe my fingers are too fat) but the issue is the same.

I tried reducing the speed of the scroll, and I even commented out 2 lines at the very bottom of the code. Commenting out the 2 lines actually seemed to do nothing, I wonder what those lines are for??

Below is the code for the frame. My python app is too complex to give you a full working page to actually play with, so sorry about that. Perhaps someone can tell just by looking at my class. Any help is appreciated!!

class VerticalScrolledFrame(tk.Frame):
    def __init__(self, parent, bg, *args, **kw):
        tk.Frame.__init__(self, parent, *args, **kw)

        # create a canvas object and a vertical scrollbar for scrolling it
        canvas = tk.Canvas(self, bd=0, highlightthickness=0, bg=bg)
        canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        # reset the view
        canvas.xview_moveto(0)
        canvas.yview_moveto(0)

        self.canvasheight = 2000

        # create a frame inside the canvas which will be scrolled with it
        self.interior = interior = tk.Frame(canvas, height=self.canvasheight, bg=bg)
        interior_id = canvas.create_window(0, 0, window=interior, anchor=tk.NW)

        # track changes to the canvas and frame width and sync them,
        # also updating the scrollbar
        def _configure_interior(event):
            # update the scrollbars to match the size of the inner frame
            size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
            canvas.config(scrollregion="0 0 %s %s" % size)
            if interior.winfo_reqwidth() != canvas.winfo_width():
                # update the canvas's width to fit the inner frame
                canvas.config(width=interior.winfo_reqwidth())
        interior.bind('<Configure>', _configure_interior)

        def _configure_canvas(event):
            if interior.winfo_reqwidth() != canvas.winfo_width():
                # update the inner frame's width to fill the canvas
                canvas.itemconfigure(interior_id, width=canvas.winfo_width())
        canvas.bind('<Configure>', _configure_canvas)

        self.offset_y = 0
        self.prevy = 0
        self.scrollposition = 1

        def on_press(event):
            self.offset_y = event.y_root
            #if self.scrollposition < 1:   #commenting out these 2 lines fixed an old scrolling issue that kept resetting the position
                #self.scrollposition = 1
            if self.scrollposition > self.canvasheight:
                self.scrollposition = self.canvasheight
            canvas.yview_moveto(self.scrollposition / self.canvasheight)

        def on_touch_scroll(event):
            nowy = event.y_root

            sectionmoved = 11             # speed of scroll
            if nowy > self.prevy:
                event.delta = -sectionmoved
            elif nowy < self.prevy:
                event.delta = sectionmoved
            else:
                event.delta = 0
            self.prevy= nowy

            self.scrollposition += event.delta
            canvas.yview_moveto(self.scrollposition/ self.canvasheight)

        #self.bind("<Enter>", lambda _: self.bind_all('<Button-1>', on_press), '+')  #what does this line do?  apparently not needed
        #self.bind("<Leave>", lambda _: self.unbind_all('<Button-1>'), '+')          ##what does this line do?  apparently not needed
        self.bind("<Enter>", lambda _: self.bind_all('<B1-Motion>', on_touch_scroll), '+')
        self.bind("<Leave>", lambda _: self.unbind_all('<B1-Motion>'), '+')
Reply
#2
Ok, I entirely commented out the on_press function and it seems to work fine.

Also I added some code to on_touch_scroll. Pushing the buttons now does not make the screen jump! But I cannot slowly scroll the screen, only can scroll with fast finger flicks now.

I guess I can live with that compromise (lost the ability to slow scroll). But there is another issue that persists - if my last touch was far from the newest touch, the screen will scroll a tiny amount towards the new touch. I would definitely like to know how to deactivate that effect.

 def on_touch_scroll(event):
            nowy = event.y_root
 
            sectionmoved = 11             # speed of scroll
            if nowy > (self.prevy + 20):    # to limit the minimum threshold to scroll
                event.delta = -sectionmoved
            elif nowy < (self.prevy - 20):  # to limit the minimum threshold to scroll
                event.delta = sectionmoved
            else:
                event.delta = 0
            self.prevy= nowy
 
            self.scrollposition += event.delta
            canvas.yview_moveto(self.scrollposition/ self.canvasheight)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 814 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 744 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,375 May-25-2023, 07:37 PM
Last Post: deanhystad
  Can't get tkinter button to change color based on changes in data dford 4 3,362 Feb-13-2022, 01:57 PM
Last Post: dford
  [Kivy] Unwanted format carryover hammer 0 1,335 Jan-14-2022, 05:17 PM
Last Post: hammer
  Creating a function interrupt button tkinter AnotherSam 2 5,416 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 4,918 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter showing image in button rwahdan 3 5,519 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  .get() invoke after a button nested press iddon5 5 3,217 Mar-29-2021, 03:55 AM
Last Post: deanhystad
  tkinter button image Nick_tkinter 4 3,958 Mar-04-2021, 11:33 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020