如何根据LabelFrame的宽度自动将Entry Widget填充到最后一个
答案 0 :(得分:1)
您需要的是columnconfigure()
和sticky='ew'
的组合。我看到您正在设置框架的高度和宽度,并通过禁用网格传播来强制它,但是我不是100%确定需要这样做。
看下面的例子。
不带grid_propagate(0)
的示例:
import tkinter as tk
root = tk.Tk()
root.geometry('250x200')
root.columnconfigure(0, weight=1) # Used to allow column 0 in root to expand
some_frame = tk.LabelFrame(root, text='Hello')
some_frame.grid(row=0, column=0, sticky='ew')
some_frame.columnconfigure(1, weight=1) # Used to allow column 1 in some_frame to expand
entry = tk.Entry(some_frame)
entry.grid(row=0, column=1, sticky='ew')
root.mainloop()
grid_propagate(0)
的示例:
import tkinter as tk
root = tk.Tk()
some_frame = tk.LabelFrame(root, text='Hello', width=250, height=200)
some_frame.grid(row=0, column=0)
some_frame.grid_propagate(0) # Not sure why you are doing this You can have the frame expand with the window as well
some_frame.columnconfigure(1, weight=1) # Used to allow column 1 in some_frame to expand
entry = tk.Entry(some_frame)
entry.grid(row=0, column=1, sticky='ew')
root.mainloop()