[英]Python Tkinter Canvas Many create_window() Items Not Scrolling with Scrollbar
Why doesn't the scrollbar initiate when create_window
frame objects start to exceed the bottom self.container
window? 为什么当create_window
框架对象开始超过底部self.container
窗口时,滚动条为什么不启动?
My understanding is that widgets are scrollable if they are embedded on the canvas using create_window
. 我的理解是,如果小部件使用create_window
嵌入在画布上,则它们是可滚动的。 For context, I don't want to create a scrolling frame - put all your widget in a frame, use create_window
to add that frame to the canvas - because I intend to move these frame objects around on the canvas and leverage a lot of canvas capabilities. 对于上下文,我不想创建滚动框架-将所有小部件放在框架中,使用create_window
将该框架添加到画布上-因为我打算在画布上四处移动这些框架对象并利用很多画布能力。 According to Effbot , You cannot draw other canvas items on top of a widget. 根据Effbot的介绍 , 您不能在小部件顶部绘制其他画布项目。 , so if I had a scrolling frame, I wouldn't be able to put widgets on top of that. ,因此,如果我有滚动框架,则无法在其上放置小部件。
So how do I scroll the canvas that contains many create_window
objects, or, what am I doing wrong below? 那么,如何滚动包含许多create_window
对象的画布,或者我在下面做错了什么?
import tkinter as tk
class Canvas_Scrollbar_CreateWindow(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.parent.columnconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
self.block_count = 0
self.button = tk.Button(self, text='Add', command=self.addblock)
self.button.grid(row=0, column=0, columnspan=2, sticky='new')
self.container = tk.Frame(self)
self.container.grid(row=1, column=0, sticky='nsew')
self.canvas = tk.Canvas(self.container, width=200, height=450)
self.scrollbar = tk.Scrollbar(self.container,
orient='vertical',command=self.canvas.yview)
self.canvas.config(yscrollcommand=self.scrollbar.set)
self.canvas.grid(row=0, column=0, sticky='nsew')
self.scrollbar.grid(row=0, column=1, sticky='nse')
self.container.bind('<Configure>', self.handle_scroll)
def addblock(self):
self.block = tk.Frame(self.canvas, bd=1, relief='solid')
self.block.columnconfigure(0, weight=1)
self.canvas.create_window((0, (self.block_count*25)),
window=self.block, anchor="nw",
width=200, height=24)
self.block_count += 1
def handle_scroll(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
root = tk.Tk()
app = Canvas_Scrollbar_CreateWindow(root)
app.grid(row=0, column=0, sticky='ew')
root.mainloop()
将项目添加到画布时,必须重新配置scrollregion
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.