Tkinter 帆布 &带网格的滚动条

问题描述

我有一个画布在一个框架中

I have a canvas in a frame

photoFrame = Frame(centerFrame, width=250, height=190, bg="#EBEBEB")
photoFrame.grid(row=0, column=1, sticky="nsew")
photoCanvas = Canvas(photoFrame, bg="#EBEBEB")
photoCanvas.grid(row=0, column=0, sticky="nsew")

我尝试用这个在我的画布上放置一个滚动条

and I try to put a scrollbar to my canvas with this

photoScroll = Scrollbar(photoFrame, orient=VERTICAL)
photoScroll.config(command=photoCanvas.yview)
photoCanvas.config(yscrollcommand=photoScroll.set)
photoScroll.grid(row=0, column=1, sticky="ns")

滚动条出现但被禁用.你能帮帮我吗?

The scrollbar appears but it's disabled. Can you help me please ?

对不起,我的英语不好.

Sorry for my bad english.

在 for 循环中,我使用此代码添加了许多图像按钮

In a for loop I add lots of Image button with this code

element = Button(photoCanvas, image = listPhotos[i], borderwidth=0, height = 200, width = 200, bg="#EBEBEB")
element.grid(row=rowPhoto, column=columnPhoto, padx=5, pady=5, sticky="nsew")

终于有了这个

    root = Tk()    
    photoFrame = Frame(root, width=250, height=190, bg="#EBEBEB")
        

    photoCanvas = Canvas(photoFrame, bg="#EBEBEB")
    photoCanvas.grid(row=0, column=0, sticky="nsew")
    
    for i in range(0, len(listPhotos), 1):
       element = Button(photoCanvas, image = listPhotos[i], borderwidth=0, height = 200, width = 200, bg="#EBEBEB")
       element.grid(row=rowPhoto, column=columnPhoto, padx=5, pady=5, sticky="nsew")
    
    photoScroll=Scrollbar(photoFrame,orient=VERTICAL)
    photoScroll.config(command=photoCanvas.yview)
    photoCanvas.config(yscrollcommand=photoScroll.set)
    photoScroll.grid(row=0, column=1, sticky="ns")

在我的应用程序中,紫色矩形是下一帧,我需要一个垂直滚动条

in my app, the purple rectangle is the next frame and I need a vertical scrollbar

有什么问题就说吧


解决方案

滚动一组小部件的一种方法是将它们(使用 packgrid)放在里面一个框架并将这个框架放在画布中.

One way to scroll a group of widgets is to put them (with grid of pack) inside a frame and put this frame inside a canvas.

滚动工作的两个关键元素(除了将滚动条连接到画布之外)是:

The two key elements (besides connecting the scrollbar to the canvas) for the scrolling to work are:

  • 使用 canvas.create_window(x, y, window=frame) 将框架放在画布内,以便将其视为画布项.
  • 使用 canvas.configure(scrollregion=canvas.bbox('all')) 每当框架大小发生变化时(例如添加新小部件后)更新画布滚动区域..李>
  • Use canvas.create_window(x, y, window=frame) to put the frame inside the canvas so that it is treated like a canvas item.
  • Update the canvas scrollregion each time the size of the frame changes (for instance after adding a new widget) with canvas.configure(scrollregion=canvas.bbox('all')).

这是对问题代码的改编Python Tkinter scrollbar for frame,但使用 OP 问题中的小部件名称和 grid 而不是 pack:

Here is an adaptation of the code of the question Python Tkinter scrollbar for frame, but using the widgets name from the OP's question and grid instead of pack:

import tkinter as tk

def update_scrollregion(event):
    photoCanvas.configure(scrollregion=photoCanvas.bbox("all"))

root = tk.Tk()   


photoFrame = tk.Frame(root, width=250, height=190, bg="#EBEBEB")
photoFrame.grid()
photoFrame.rowconfigure(0, weight=1) 
photoFrame.columnconfigure(0, weight=1) 

photoCanvas = tk.Canvas(photoFrame, bg="#EBEBEB")
photoCanvas.grid(row=0, column=0, sticky="nsew")

canvasFrame = tk.Frame(photoCanvas, bg="#EBEBEB")
photoCanvas.create_window(0, 0, window=canvasFrame, anchor='nw')

for i in range(10):
   element = tk.Button(canvasFrame, text='Button %i' % i, borderwidth=0, bg="#EBEBEB")
   element.grid(padx=5, pady=5, sticky="nsew")

photoScroll = tk.Scrollbar(photoFrame, orient=tk.VERTICAL)
photoScroll.config(command=photoCanvas.yview)
photoCanvas.config(yscrollcommand=photoScroll.set)
photoScroll.grid(row=0, column=1, sticky="ns")

canvasFrame.bind("<Configure>", update_scrollregion)

root.mainloop()

相关文章