Python Forum
[Tkinter] canvas widget scroll issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] canvas widget scroll issue
#1
Hi,

I have a canvas inside a frame that contains 3 entry widgets, but for some reason ,when I scroll up, the widgets move down, althoug there is no space above these entries.

Below is my code:
import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master: tk.Tk = None):
        super().__init__(master)
        self.master = master
        self.grid(row=0)
        self.create_widgets()

    def create_widgets(self):
        # Table view with entries in scrollable frame
        self.scroll_frame = ScrollableFrame(self.master)
        self.scroll_frame.grid(row=4, column=0,  columnspan = 5, padx = (10, 10), sticky="W")
        
        tab_entry = tk.Entry(self.scroll_frame.scrollable_frame, relief=tk.GROOVE, width = 191)
        tab_entry.grid(row=1, column=0, sticky=tk.NSEW)
        tab_entry1 = tk.Entry(self.scroll_frame.scrollable_frame, relief=tk.GROOVE, width = 191)
        tab_entry1.grid(row=2, column=0, sticky=tk.NSEW)
        tab_entry2 = tk.Entry(self.scroll_frame.scrollable_frame, relief=tk.GROOVE, width = 191)
        tab_entry2.grid(row=3, column=0, sticky=tk.NSEW)

class ScrollableFrame(tk.Frame):
    """ Create a scrollable frame for use with 'Entry' widgets in table form
        
    Attributes:
        canvas (tkinter.Canvas)
        scrollable_frame (tkinter.Frame): frame for use inside canvas to enable scrolling of widgets
    """
    def __init__(self, container: tk.Tk, *args, **kwargs):
        super().__init__(container, *args, **kwargs)

        self.canvas = tk.Canvas(self, scrollregion=(0, 0, 1150, 180), width = 1150, height = 180, bg='white' )

        scrollbar_y = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
        scrollbar_x = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)
        
        # reset the view
        self.canvas.xview_moveto(0)
        self.canvas.yview_moveto(0)

        self.scrollable_frame = tk.Frame(self.canvas)

        self.scrollable_frame.bind(
            "<Configure>",
            lambda e: self.canvas.configure(
                scrollregion = self.canvas.bbox("all")
            )
        )

        self.canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")

        self.canvas.configure(yscrollcommand=scrollbar_y.set, xscrollcommand=scrollbar_x.set)
        
        self.canvas.bind_all("<MouseWheel>", self._on_mousewheel)

        self.canvas.grid(row=0, column=0, sticky="news")
        scrollbar_y.grid(row=0, column=1, sticky='ns')
        scrollbar_x.grid(row=1, column=0, sticky='ew')
        
        
        
    def _on_mousewheel(self, event: tk.Event):
        self.canvas.yview_scroll(int(-1*(event.delta/120)), "units")

        
if __name__ == '__main__':
    root = tk.Tk()
    root.title("test")
    root.geometry('{}x{}+{}+{}'.format(1200, 620, 40, 40))
    root.resizable(0,0)
    app = Application(master=root)
    app.mainloop()
Is there a reason why this strange scroll issue occurs?
I'm running this on Windows with Python 3.9.

Thank you
Reply


Messages In This Thread
canvas widget scroll issue - by chrisdb - Apr-02-2021, 01:02 PM
RE: canvas widget scroll issue - by deanhystad - Apr-04-2021, 10:42 PM
RE: canvas widget scroll issue - by chrisdb - Apr-07-2021, 05:48 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] QTableView: scroll to top cell of the screen random_nick 2 2,948 Oct-08-2022, 12:29 AM
Last Post: random_nick
  [PyQt] How do I get a QScrollArea to scroll? LavaCreeperKing 9 8,033 Oct-29-2021, 08:33 AM
Last Post: Axel_Erfurt
  Treeview scroll selected node to top rfresh737 1 2,759 Apr-14-2021, 03:27 AM
Last Post: deanhystad
  Entry Widget issue PA3040 16 7,034 Jan-20-2021, 02:21 PM
Last Post: pitterbrayn
  [Tkinter] Help with scroll bars kraco 1 2,269 Sep-27-2020, 11:20 PM
Last Post: Larz60+
  How to place global tk text widget in class or on canvas puje 1 2,377 Jul-04-2020, 09:25 AM
Last Post: deanhystad
  Matplotlib figsize and centering issue on canvas deffonotsean 0 1,869 Jun-10-2020, 10:31 AM
Last Post: deffonotsean
  [Tkinter] How to place scroll bar correctly scratchmyhead 1 3,997 May-18-2020, 04:17 PM
Last Post: scratchmyhead
  Scroll frame with MouseWheel Nemesis 1 3,687 Mar-25-2020, 09:29 PM
Last Post: Nemesis
  The coordinates of the Entry widget (canvas) when moving berckut72 8 6,097 Jan-07-2020, 09:26 AM
Last Post: berckut72

Forum Jump:

User Panel Messages

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