0

I am trying to place a scrollbar on the bottom frame of my window. When I run this code, the scrollbar is placed in the middle of the screen and very small, until I use my mouse wheel and actually scroll around, then it goes to the right side of the screen and acts normally. Although, for some reason also when it goes to the right side of the screen, there is then a white border around my bottom frame, which is very frustrating.

I need help getting my scrollbar to appear on the right side of the bottom frame as well as not having the white border around my bottom frame.

import tkinter as tk

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


def gui_setup(root):
    root.title('Test')
    root.config(bg='snow3')
    root.minsize(600, 700)

    top_frame = tk.Frame(root, width=600, height=300)
    top_frame.grid(row=0, column=0)


    btm_frame = tk.Frame(root, width=600, height=400)
    btm_frame.config(bg='snow3', borderwidth=0)
    btm_frame.grid(row=2, column=0)

    global bottom_area
    global canvas
    canvas = tk.Canvas(btm_frame, borderwidth=0, background='snow3')
    canvas.config(width=600, height=420)
    bottom_area = tk.Frame(canvas, background='snow3', borderwidth=0)

    scroll = tk.Scrollbar(canvas, orient='vertical', command=canvas.yview)
    canvas.config(yscrollcommand=scroll.set)
    scroll.pack(side='right', fill='y')


    def on_mouseWheel(event):
        shift = (event.state & 0x1) != 0
        scroll_action = -1 if event.delta > 0 else 1
        if shift:
            canvas.xview_scroll(scroll_action, 'units')
        else:
            canvas.yview_scroll(scroll_action, 'units')

    canvas.pack(side='left', fill='both', expand=True)
    canvas.create_window((4,4), window=bottom_area, anchor='nw')
    bottom_area.bind('<Configure>', lambda event, canvas=canvas: onFrameConfigure(canvas=canvas))
    bottom_area.bind_all('<MouseWheel>', on_mouseWheel)


root = tk.Tk()
gui_setup(root)
tk.mainloop()
Lzypenguin
  • 945
  • 1
  • 7
  • 18
  • Not really a Canvas expert but why are you using `scroll.pack` instead of `canvas.create_window`? – TheLizzard Mar 06 '21 at 22:51
  • I have no reason besides the fact that I found this method online and was able to successfully implement it into my project. I am not sure how i would implement canvas.create_window. – Lzypenguin Mar 06 '21 at 23:06
  • It's very rare to use `create_window` on a scrollbar. It's also very rate to make the scrollbar a child of the thing it is scrolling. Typically scrollbars live outside of the widget they are controlling. – Bryan Oakley Mar 07 '21 at 00:04
  • Try change the parent of the scrollbar `btm_frame`. Also consider using the class I created [here](https://stackoverflow.com/a/66215091/11106801). I think that it is exactly what you need – TheLizzard Mar 07 '21 at 11:04

0 Answers0