alw*_*btc 2 python size tkinter widget
我创建了一个 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)
Run Code Online (Sandbox Code Playgroud)

但是当我在其中插入一个空字符串时,errorArea小部件的大小会根据插入的字符串进行调整:
errorMessage = Tkinter.Label(errorArea, text="")
errorMessage.grid(row=0, column=0, padx=5, pady=2, sticky='W')
Run Code Online (Sandbox Code Playgroud)

我如何给errorArea小部件一个固定的大小,以便它的大小不会根据插入的标签而改变?
小智 5
这个问题对我来说总是很有趣。我发现修复它的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()
Run Code Online (Sandbox Code Playgroud)
这样,标签被放置在窗口中,而不会缩小标签框。
| 归档时间: |
|
| 查看次数: |
11133 次 |
| 最近记录: |