您如何在 Tkinter 中将 columnconfigure 与可滚动框架一起使用?

How do you use columnconfigure with a scrollable frame in Tkinter?

我正在尝试创建一个可滚动的框架,其中的网格以不同的权重管理其中的小部件。但是,columnconfigure 似乎并未影响可滚动框架内的小部件。在此代码片段中,我使用的是 Pmw.ScrollableFrame,但我也尝试过 tkscrolledframe 和此处的代码大纲:https://blog.teclado.com/tkinter-scrollable-frames/,但它们似乎都有相同的问题。

from Pmw import ScrolledFrame

root = Tk()
root.state("zoomed")

Grid.columnconfigure(root, 0, weight = 4)
Grid.columnconfigure(root, 1, weight = 5)

Grid.rowconfigure(root, 0, weight = 1)
Grid.rowconfigure(root, 1, weight = 15)
Grid.rowconfigure(root, 2, weight = 3)

sf = ScrolledFrame(root)

Grid.columnconfigure(sf.interior(), index = 0, weight = 1)
Grid.columnconfigure(sf.interior(), index = 1, weight = 100)

for i in range(100):
    Button(sf.interior(), text = "button 1").grid(row = i, column = 0, sticky = 'nsew')
    Button(sf.interior(), text = "button 2").grid(row = i, column = 1, sticky = 'nsew')
    
sf.grid(row = 1, column = 0, sticky = 'nsew')

Button(root, text = "button 2").grid(row = 1, column = 1, sticky = 'nsew')
Button(root, text = "button 3").grid(row = 2, column = 0, sticky = 'nsew', columnspan = 2)
Button(root, text = "button 4").grid(row = 0, column = 1, sticky = 'nsew')

root.mainloop()

如果我根本无法在这些类型的框架上使用 columnconfigure,我还能使用什么来控制可滚动框架内小部件的大小而无需对大小进行硬编码?任何帮助将不胜感激!

编辑补充:我打算用输入框、复选框和选项菜单的组合来填充可滚动框架。

您只需更改值即可将 scrolltext 的内置函数与 TextboxFrame 一起使用。如果你想放别的东西,你可能想使用 insertdeleteStringVariable().

import tkinter

root = Tk()
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
textbox = Text(root)
textbox.pack()
for i in range(100):
    textbox.insert(END, f"Scrolling Text Label {i}\n")
textbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=textbox.yview)
root.mainloop()

编辑:如果你想在滚动条上进行检测,因为你正在使用 PMW 你可以使用代表 scrolledbinding 系统。

问题不在于 columnconfigure。问题是内部框架没有扩展以填充 window.

Pmw.ScrolledFrame class 有一个 documented attribute horizflex 可以设置为 expand 来填充 window.

horizflex Specifies how the width of the scrollable interior frame should be resized relative to the clipping frame. If 'fixed', the interior frame is set to the natural width, as requested by the child widgets of the frame. If 'expand' and the requested width of the interior frame is less than the width of the clipping frame, the interior frame expands to fill the clipping frame. If 'shrink' and the requested width of the interior frame is more than the width of the clipping frame, the interior frame shrinks to the width of the clipping frame. If 'elastic', the width of the interior frame is always set to the width of the clipping frame. The default is 'fixed'.

听起来您应该将其设置为 expand

sf = ScrolledFrame(root, horizflex="expand")