我目前正在挣扎使用滚动框架内的Tkinter画布。通过滚动框架,我的意思是Gonzo给另一个线程(Python Tkinter scrollbar for frame)给出的框架 - 画布结构。我已经适应了这个概念并添加了水平滚动条。现在,在内部框架的顶部,我想放置一个具有给定最小尺寸的画布。在这个最小尺寸小于内部框架(或整个窗口)的情况下,没有问题(因为不需要滚动)。但是,如果我将窗口大小调整为小于画布的值(这是滚动条应该有用的条件),则画布不会被缩小到所需的大小(在下面的示例中,不是宽度= 500px),而是在帧的较小尺寸(宽度= 400px)时被截断。滚动条处于活动状态,滚动仍处于500px的内部框架。滚动框架内的画布
下面我给你一些关于代码和问题的更多细节。
谢谢你的关注。我很感谢这个问题的任何帮助,也许我只是在做一些明显错误的事情。
干杯, C.
ScrollFrame类(存储在scrollframe.py):
from Tkinter import *
class ScrollFrame(Frame):
def __init__(self, parent, yscroll=True, xscroll=True, *args, **kw):
Frame.__init__(self, parent, *args, **kw)
self.canvas = Canvas(self, bd=0, highlightthickness=0)
if xscroll:
hscrollbar = Scrollbar(self, orient=HORIZONTAL)
hscrollbar.pack(fill=X, side=BOTTOM, expand=FALSE)
self.canvas.config(xscrollcommand=hscrollbar.set)
hscrollbar.config(command=self.canvas.xview)
if yscroll:
vscrollbar = Scrollbar(self, orient=VERTICAL)
vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE)
self.canvas.config(yscrollcommand=vscrollbar.set)
vscrollbar.config(command=self.canvas.yview)
self.canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
# reset the view
self.canvas.xview_moveto(0)
self.canvas.yview_moveto(0)
# create a frame inside the canvas which will be scrolled with it
self.interior = interior = Frame(self.canvas, bg="blue")
self.interior_id = self.canvas.create_window(0, 0, window=interior, anchor=NW)
# track changes to the canvas and frame width and sync them,
# also updating the scrollbar
def _configure_interior(event):
# update the scrollbars to match the size of the inner frame
size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
print "interior req: ",size,(interior.winfo_width(),interior.winfo_height())
self.canvas.config(scrollregion="0 0 %s %s" % size)
if interior.winfo_reqwidth() != self.canvas.winfo_width() or interior.winfo_reqheight() != sel f.canvas.winfo_height():
# update the canvas's width to fit the inner frame
self.canvas.config(width=interior.winfo_reqwidth(), height=interior.winfo_reqheight(),)
interior.bind('<Configure>', _configure_interior)
def _configure_canvas(event):
if interior.winfo_reqwidth() != self.canvas.winfo_width() or interior.winfo_reqheight() != sel f.canvas.winfo_height():
# update the inner frame's width to fill the canvas
self.canvas.itemconfigure(self.interior_id, width=self.canvas.winfo_width(), height=self.c anvas.winfo_height())
self.canvas.bind('<Configure>', _configure_canvas)
的主要代码: 从Tkinter的进口* 从scrollframe进口*
root = Tk()
root.geometry("400x300")
frame = Frame(root, bg="blue")
frame.pack(expand=True, fill=BOTH)
sf = ScrollFrame(frame, bg="yellow")
sf.pack(expand=True, fill=BOTH)
sf.interior.config(width=500, height=400)
canvas = Canvas(sf.interior, width=500, height=400, bg="green")
canvas.pack(expand=True, fill=BOTH, side="top")
root.mainloop()
运行时,看起来像这样: 1. canvas at top left corner (not yet scrolled) 2. canvas at bottom right corner (after scrolling)
问题:绿色帆布应该是所需的尺寸(500/400),但它似乎是滚动条连接到(400/300)而不是内部的祖母框架的尺寸框架,它是滚动的(500/400),它是画布的母框。