插入空标签时如何防止Tkinter labelframe大小更改

时间:2013-08-03 21:17:39

标签: python widget tkinter size

我创建了一个LabelFrame小部件。它的开头尺寸不错:

import Tkinter
form = Tkinter.Tk()

errorArea = Tkinter.LabelFrame(form, text=" Errors ", width=250, height=80)
errorArea.grid(row=2, column=0, columnspan=2, sticky="E", \
             padx=5, pady=0, ipadx=0, ipady=0)

enter image description here

但是当我在其中插入一个空字符串时,errorArea小部件的大小会根据插入的字符串进行调整:

errorMessage = Tkinter.Label(errorArea, text="")
errorMessage.grid(row=0, column=0, padx=5, pady=2, sticky='W')

enter image description here

如何为errorArea窗口小部件提供固定大小,以便其大小不会根据插入的Lable进行更改?

3 个答案:

答案 0 :(得分:3)

这个问题对我来说总是很有趣。我发现修复它的一种方法是使用place方法而不是grid

import Tkinter

form = Tkinter.Tk()

errorArea = Tkinter.LabelFrame(form, text=" Errors ", width=250, height=80)
errorArea.grid(row=2, column=0, columnspan=2, sticky="E", \
             padx=5, pady=0, ipadx=0, ipady=0)

errorMessage = Tkinter.Label(errorArea, text="")

# 1) 'x' and 'y' are the x and y coordinates inside 'errorArea'
# 2) 'place' uses 'anchor' instead of 'sticky'
# 3) There is no need for 'padx' and 'pady' with 'place'
# since you can specify the exact coordinates
errorMessage.place(x=10, y=10, anchor="w")

form.mainloop()

这样,标签就会放在窗口中,而不会缩小labelframe。

答案 1 :(得分:1)

如果使用粘性值将窗口小部件粘贴到其单元格的所有四个边而不仅仅是一个边,则当您在其中放置一个小标签窗口小部件时,它不会缩小。

另一种选择是调用errorArea.grid_propagate(False),它告诉网格区域不缩小或展开以适应其内容。这通常会导致不合需要的调整大小行为,或者至少需要您做一些额外的工作才能获得正确的调整大小行为。

答案 2 :(得分:0)

在声明Labelframe之后立即使用网格功能。

EX:

String_l = ttk.Labelframe(pw, text='String',width=100, height=408).grid(column=1, row=0, padx=4, pady=4,rowspan=2)