Tkinter Scrollbar not working

吃可爱长大的小学妹 提交于 2019-12-11 16:24:16

问题


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 be linked to the size of the frame. The code I'm using is essentially:

class EntryWindow:
    def __init__(self, master):
        self.master = master
        self.master.minsize(750, 800)
        self.master.maxsize(1000, 800)
        self.canvas = tk.Canvas(self.master, borderwidth=0, bg='#ffffff')
        self.vsb = tk.Scrollbar(self.master)
        self.master_frame = tk.Frame(self.canvas)
        self.vsb.pack(side="right", fill='y')
        self.canvas.pack(side='left', fill='both', expand=True)
        self.canvas.create_window((0,0), window=self.master_frame, anchor='nw', tags='self.master_frame')
        self.canvas.config(yscrollcommand=self.vsb.set)
        self.master_frame.grid()

        ###build widgets and place into master_frame
        ...

The master_frame is filled with about 36 widgets placed using the grid geometry manager and reaches a vertical height of about 2000 pixels, but the scrollbar doesn't work.

I saw a post about using ttk scrollbar, but when I imported that I couldn't get it to work. The input statement I used was:

import tkinter as tk
import tkinter.ttk as ttk

and then replaced the line self.vsb = tk.Scrollbar(self.master) with self.vsb = ttk.Scrollbar(self.master) but that didn't fix it either. I also tried removing the min/max sizing on master (those aren't the final values, I've been playing with it).

Is there something I'm doing wrong? I feel like it might be in the line where I set the canvas.config() but I read the documentation and it seems to be right. Tomorrow I plan on trying to load the frame into the canvas after I've built the frame.

But in the meantime, and in case that doesn't work, any help would be great! Thanks!


回答1:


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



来源:https://stackoverflow.com/questions/38780724/tkinter-scrollbar-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!