简体   繁体   English

Tkinter滚动条不起作用

[英]Tkinter Scrollbar not working

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. 我在python 3.4上运行了一段tkinter代码,这是一个放置在带有垂直滚动条的画布中的大框架,但是该滚动条是灰色的,似乎与框架的大小无关。 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. master_frame充满了使用网格几何管理器放置的约36 master_frame部件,并且达到了约2000像素的垂直高度,但是滚动条不起作用。

I saw a post about using ttk scrollbar, but when I imported that I couldn't get it to work. 我看到了有关使用ttk滚动条的帖子,但是当我导入该帖子时,我无法使其正常工作。 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. 再换成线self.vsb = tk.Scrollbar(self.master)self.vsb = ttk.Scrollbar(self.master)但没有任何解决它。 I also tried removing the min/max sizing on master (those aren't the final values, I've been playing with it). 我还尝试删除了master的最小/最大大小(这些不是最终值,我一直在使用它)。

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. 我觉得可能是在我设置canvas.config()的那一行中,但是我阅读了文档,似乎是正确的。 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! 谢谢!

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 通过配置画布的yscrollcommand属性使其指向滚动条,使画布在滚动时通知滚动条
  2. have the scrollbar control the canvas when it is dragged, by configuring the command attribute of the scrollbar 通过配置滚动条的command属性,使滚动条在拖动时控制画布
  3. tell the canvas what part of the virtual canvas should be scrollable by configuring the scrollregion attribute of the canvas 通过配置画布的scrollregion属性,告诉画布虚拟画布的哪一部分应可滚动

You are neglecting to do #3. 您忽略了做#3。 After adding the widgets to master_frame you should do this: 将小部件添加到master_frame您应该执行以下操作:

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 . 最后,您需要删除以下代码行,因为您已经使用create_window将框架添加到画布中。 Frames added to a canvas with pack , place or grid won't scroll: 添加到带有packplacegrid的画布的框架将不会滚动:

# 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 有关可滚动框架的工作示例,请参阅在Tkinter中将滚动条添加到一组小部件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM