Tkinter canvas amp; scrollbar with grid(Tkinter 帆布 amp;带网格的滚动条)
问题描述
我有一个画布在一个框架中
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
有什么问题就说吧
推荐答案
滚动一组小部件的一种方法是将它们(使用 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 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()
这篇关于Tkinter 帆布 &带网格的滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Tkinter 帆布 &带网格的滚动条
基础教程推荐
- 筛选NumPy数组 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01