根据窗口小部件大小停止调整Tkinter帧的大小

Jig*_*uff 8 python tkinter

root = Tk()

descriptionFrame = Frame(root)

definitionFrame = LabelFrame(descriptionFrame, text="Definition")
definitionScroll = Scrollbar(definitionFrame)
definitionCanvas = Canvas(definitionFrame, width=30, height=4, yscrollcommand=definitionScroll.set)
definitionScroll.config(command=definitionCanvas.yview)
definitionLabel = Label(definitionCanvas, text="n/a")

descriptionFrame.pack()
definitionFrame.pack()
definitionScroll.pack(side=RIGHT, fill=Y)
definitionCanvas.pack(side=LEFT, fill=BOTH, expand=True)
definitionLabel.pack(fill=BOTH, expand=True)

root.mainloop()
Run Code Online (Sandbox Code Playgroud)

我有这个代码.Canvas设置为宽度为30,高度为4,但是当我运行它时,它会忽略Canvas的宽度和高度,而生成的窗口会在Label周围调整大小.我已经尝试pack_propagate(False)在代码中的每一个帧上使用它,但它不会影响任何东西definitionFrame,但是当我在descriptionFrame它上面使用它时会导致一个空窗口.如何创建一个GUI,其中所有框架和窗口的大小均为宽度30和高度4的画布大小?

谢谢.

Bry*_*ley 10

要回答有关如何停止框架(或任何容器窗口小部件)的特定问题,请调整其大小以适应其内容,您可以调用pack_propagate(False)grid_propagate(False)依赖于您正在使用的几何管理器.如果你已经尝试过并且它无法正常工作,那你做错了.由于您没有发布该代码,我们无法诊断出错了什么.

当你打电话时,pack_propagate(False)你必须确保小部件有适当的大小.标签和按钮将具有适合其文本的默认大小,但框架的默认大小为1x1,使内容几乎不可见.如果在框架上使用此功能,请确保为其指定明确的宽度和高度.


Shi*_*ish 3

默认情况下,只有 Listbox、Text、Canvas 和 Entry 是可滚动的;Canvas 可以工作,但在我看来有点矫枉过正,所以这看起来像是你想要使用 Text 实现的东西

#!/usr/bin/python
from Tkinter import *
root = Tk()

descriptionFrame = Frame(root)

definitionFrame = LabelFrame(descriptionFrame, text="Definition")
definitionScroll = Scrollbar(definitionFrame)
definitionText = Text(definitionFrame, width=30, height=4, yscrollcommand=definitionScroll.set)
definitionScroll.config(command=definitionText.yview)

definitionText.delete("1.0", END)   # an example of how to delete all current text
definitionText.insert("1.0", "n/a") # an example of how to add new text to the text area

descriptionFrame.pack(fill=BOTH, expand=True)
definitionFrame.pack(fill=BOTH, expand=True)
definitionScroll.pack(side=RIGHT, fill=Y)
definitionText.pack(side=LEFT, fill=BOTH, expand=True)

root.mainloop()
Run Code Online (Sandbox Code Playgroud)