gpt4 book ai didi

python - Tkinter动态 "iframe"不会滚动

转载 作者:行者123 更新时间:2023-12-01 05:25:33 24 4
gpt4 key购买 nike

我写了一些代码,但我无法弄清楚为什么它不起作用。我正在尝试制作一个带有动态内容滚动条的框架,但由于某种原因,滚动条不会滚动。

import scrolledframe
from Tkinter import *

class app(Tk):
def __init__(self, parent):
Tk.__init__(self, parent)
self.parent=parent
self.initialize()
populateinterior(self.sframe.interior)
def initialize(self):
self.grid()
self.grid_columnconfigure(0, weight=1)
self.grid_rowconfigure(0,weight=1)
self.resizable(True,True)
self.sframe = scrolledframe.VerticalScrolledFrame(self)
self.sframe.grid()

def OnButtonClick():
appinstance.sframe.interior.buttons.append(Button(appinstance.sframe.interior, text=u'Click meh!',command=OnButtonClick))
appinstance.sframe.interior.buttons[-1].grid(row=len(appinstance.sframe.interior.buttons)+1,column=0)
def populateinterior(self):
self.grid()
self.buttons=[]

self.buttons.append(Button(self, text=u'Click meh!',command=OnButtonClick))
self.buttons[-1].grid(row=len(self.buttons)+1,column=0)

if __name__=="__main__":
appinstance=app(None)
appinstance.title("All dem appz belong to meh")
appinstance.mainloop()

scrolledframe 不是我自己写的,但无论如何它是这样的:

from Tkinter import *
class VerticalScrolledFrame(Frame):
"""A pure Tkinter scrollable frame that actually works!

* Use the 'interior' attribute to place widgets inside the scrollable frame
* Construct and pack/place/grid normally
* This frame only allows vertical scrolling

Assuming this is Public domain, but it's from here: http://tkinter.unpythonic.net/wiki/VerticalScrolledFrame

"""
def __init__(self, parent, *args, **kw):
Frame.__init__(self, parent, *args, **kw)

# create a canvas object and a vertical scrollbar for scrolling it
vscrollbar = Scrollbar(self, orient=VERTICAL)
vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE)
canvas = Canvas(self, bd=0, highlightthickness=0,
yscrollcommand=vscrollbar.set)
canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
vscrollbar.config(command=canvas.yview)

# reset the view
canvas.xview_moveto(0)
canvas.yview_moveto(0)

# create a frame inside the canvas which will be scrolled with it
self.interior = interior = Frame(canvas)
interior_id = 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())
canvas.config(scrollregion="0 0 %s %s" % size)
if interior.winfo_reqwidth() != canvas.winfo_width():
# update the canvas's width to fit the inner frame
canvas.config(width=interior.winfo_reqwidth())
interior.bind('<Configure>', _configure_interior)

def _configure_canvas(event):
if interior.winfo_reqwidth() != canvas.winfo_width():
# update the inner frame's width to fill the canvas
canvas.itemconfigure(interior_id, width=canvas.winfo_width())
canvas.bind('<Configure>', _configure_canvas)

return

有什么线索吗?(我知道这是丑陋的代码,但这只是概念证明)

最佳答案

问题出在 populateinterior 中的第一行:

def populateinterior(self):
self.grid()
...

self 在这种情况下是滚动框架的内部小部件。您正在对其调用grid,这是您不应该做的。内部框架不得包装、放置或网格化在 Canvas 内。相反,它必须是使用 canvas 方法 create_window 创建的 Canvas 对象(由 VerticalScrolledFrame 类处理)。当您在内部框架上调用grid时,您就破坏了 Canvas 和框架之间的结合。

关于python - Tkinter动态 "iframe"不会滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21416851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com