我已经问过这个问题,但是只得到pack-Manager的答案。
我想使用grid-Method和grid_columnconfigure / grid_rowconfigure在python中使用tkinter创建GUI。不幸的是,这在Frame内部无法正常工作。
from tkinter import *
master = Tk()
master.state('zoomed')
f = Frame(master, width=800, height=400)
Label1 = Label(f, text='Label 1')
Label2 = Label(f, text='Label 2')
f.grid_columnconfigure(0, weight=1)
f.grid_columnconfigure(2, weight=1)
f.grid_columnconfigure(4, weight=1)
Label1.grid(row=0, column=1)
Label2.grid(row=0, column=3)
master.grid_rowconfigure(0, weight=1)
master.grid_rowconfigure(2, weight=1)
master.grid_columnconfigure(0, weight=1)
master.grid_columnconfigure(2, weight=1)
f.grid(row=1, column=1)
master.mainloop()
我希望两个标签之间有空间,但这是行不通的,因为Frame不会在master内部占用更多空间。我该怎么办?
答案 0 :(得分:0)
这对我有用-但使用pack()
框架不会将尺寸更改为标签尺寸:
f.grid_propagate(False)
其中“框架”所在的列和行将使用所有空间(因为没有其他列和行)
master.grid_rowconfigure(1, weight=1)
master.grid_columnconfigure(1, weight=1)
框架将调整为列和行的大小(已经使用了窗口中的所有空间)
f.grid(..., sticky='news')
为了测试代码,我添加了背景色-它显示了小部件的实际大小。
代码:
from tkinter import *
master = Tk()
master['bg'] = 'red'
master.grid_rowconfigure(1, weight=1)
master.grid_columnconfigure(1, weight=1)
f = Frame(master, width=400, height=300)
f.grid(row=1, column=1, sticky='news')
f.grid_propagate(False)
f.grid_columnconfigure(0, weight=1)
f.grid_columnconfigure(2, weight=1)
f.grid_columnconfigure(4, weight=1)
l1 = Label(f, text='Label 1', bg='green')
l2 = Label(f, text='Label 2', bg='green')
l1.grid(row=0, column=1)
l2.grid(row=0, column=3)
master.mainloop()
如果您删除width=400, height=300
,则窗口开始时将没有大小。