How do I automatically scroll to the top of the frame after it is cleared?

Viewed 18

I have a screen that I load a bunch of data to via excel. Then I have the option to clear the screen and do it again. The problem is, when I load again, if the frame was not scrolled back to the top, the screen is laggy and jittery until it gets back to the position it was at, or until the excel file is finished loading.

What I want to know is what do I need to add into my clear function, to reset the scrollbar to the top of the screen so that this does not happen anymore.

This minimal reproducible example just shows the scrollbar not going to the top when the screen is cleared.

Thanks

import tkinter as tk

class test(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.title('TEST')
        self.config(bg='snow3')
        self.minsize(350, 250)
        self.resizable(False, False)

        def onFrameConfigure(self, canvas):
            '''Reset the scroll region to encompass the inner frame'''
            canvas.configure(scrollregion=canvas.bbox('all'))

        self.excel_upload_frame = tk.Frame(self)
        self.excel_upload_frame.config(height=650)
        self.excel_upload_frame.grid(row=4, column=0, columnspan=10, rowspan=100, sticky='nsw')

        self.canvas = tk.Canvas(self.excel_upload_frame, borderwidth=0)
        self.scroll_area = tk.Frame(self.canvas)

        self.scroll = tk.Scrollbar(self.excel_upload_frame, orient='vertical', command=self.canvas.yview)
        self.canvas.config(width=530, height=493, yscrollcommand=self.scroll.set)
        self.scroll_area.pack(fill=tk.BOTH)

        self.canvas.pack(side='left', fill=tk.Y)
        self.canvas.create_window((4, 4), window=self.scroll_area, anchor='nw')
        self.scroll_area.bind('<Configure>',
                              lambda event, canvas=self.canvas: onFrameConfigure(self, canvas=canvas))

        upload_button = tk.Button(self, text='Upload Excel File', font=('helvetica', '16'), bg='green',
                                  command=lambda: test.excel_upload_page(self))
        upload_button.grid(row=1, column=0, columnspan=10, pady=10)

        clear_screen_btn = tk.Button(self, text='    Clear    ', bg='yellow', command=lambda: test.clear_screen(self))
        clear_screen_btn.grid(row=1, column=7, columnspan=2, pady=10)


    def excel_upload_page(self):
        for widgets in self.scroll_area.winfo_children():
            widgets.destroy()
        '''Reading the excel file and making it a pandas DF'''

        for x in range(0,200):
            label = tk.Label(self.scroll_area, text=f'TEST - {x}')
            label.grid(row=x, column=0, pady=10)
            self.scroll.pack(side='right', fill='y')

    def clear_screen(self):
        for row in self.scroll_area.grid_slaves():
            row.grid_forget()




if __name__ == "__main__":
    app = test()
    app.mainloop()
1 Answers

You can call self.canvas.yview_moveto(0) at the end of both excel_upload_page() and clear_screen() functions to scroll back to the top.

def excel_upload_page(self):
    for widgets in self.scroll_area.winfo_children():
        widgets.destroy()
    '''Reading the excel file and making it a pandas DF'''

    for x in range(0,200):
        label = tk.Label(self.scroll_area, text=f'TEST - {x}')
        label.grid(row=x, column=0, pady=10)
        self.scroll.pack(side='right', fill='y')

    self.canvas.yview_moveto(0) # scroll back to the top

def clear_screen(self):
    for row in self.scroll_area.grid_slaves():
        row.grid_forget()

    self.canvas.yview_moveto(0) # scroll back to the top
Related