Tkinter Scrollbar not working

前端 未结 1 1117

I have a piece of tkinter code running on python 3.4 that is a large frame placed in a canvas with a vertical scrollbar, however the scrollbar is grayed out and doesn\'t seem to

相关标签:
1条回答
  • 2021-01-25 04:27

    You must do three things when configuring a scrolling canvas:

    1. have the canvas notify the scrollbar when it scrolls, by configuring the yscrollcommand attribute of the canvas to point to the scrollbar
    2. have the scrollbar control the canvas when it is dragged, by configuring the command attribute of the scrollbar
    3. tell the canvas what part of the virtual canvas should be scrollable by configuring the scrollregion attribute of the canvas

    You are neglecting to do #3. After adding the widgets to master_frame you should do this:

    self.canvas.configure(scrollregion=self.canvas.bbox("all")
    

    The above will tell the canvas and scrollbar that the area of the canvas that is scrollable is the area that encompasses all of the objects on the canvas.

    Finally, you need to remove the following line of code, because you are already adding the frame to the canvas with create_window. Frames added to a canvas with pack, place or grid won't scroll:

    # remove this line
    self.master_frame.grid()
    

    For a working example of a scrollable frame, see Adding a scrollbar to a group of widgets in Tkinter

    0 讨论(0)
提交回复
热议问题