Tkinter帆布&网格滚动条 [英] Tkinter canvas & scrollbar with grid
问题描述
我在框架中有一块画布
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
如果您有任何疑问,请说
Say if you have some questions
推荐答案
滚动一组小部件的一种方法是将它们(与pack
的grid
一起)放在框架中,然后将此框架放入画布中.
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滚动条 ,但使用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()
这篇关于Tkinter帆布&网格滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!