努力布局我的 Tkinter 应用程序

Struggling to layout my Tkinter application

我需要帮助来构建我的 Tkinter 程序,使其看起来像这样

这是我的代码:

import tkinter as tk
from tkinter import Grid


#GUI
window  = tk.Tk()
Grid.rowconfigure(window, 0, weight=1)
Grid.columnconfigure(window, 0, weight=1)

#set up window
height = window.winfo_screenheight()
width = window.winfo_screenwidth()
window.geometry(f'{int(width/2)}x{int(height/2)}')

frm_window = tk.Frame(window)
frm_window.grid(row=0,column=0,sticky="nsew")

                #title section
frm_title = tk.Frame(frm_window)
frm_title.grid(row=0,column=0,sticky="nsew")
lbl_title = tk.Label(master = frm_title,text="question")
lbl_title.pack()

                #table section
frm_table = tk.Frame(frm_window)
frm_table.grid(row=1,column=0,sticky="nsew")
tbl_title  = tk.Label(frm_table,text="subtitle",relief=tk.RAISED)
tbl_title.grid(row=0,column=0,columnspan=5,sticky="ew")

#row 1
l1 = ['item','item','item','item','item']
for j in range(0,len(l1)):
    name = l1[j]
    cell = tk.Label(frm_table,text=name,relief=tk.RAISED)
    cell.grid(row=1,column=j,sticky="nsew")

#row2
l2 = ['item','item','item','item','item']
for i in range(0,len(l2)):
    x = l2[i]
    cell2 = tk.Label(frm_table,text=x,relief=tk.RAISED)
    cell2.grid(row=2,column=i,sticky="nsew")
        
window.mainloop()

目前,这是输出:

我计划在 table 下添加更多小部件,因此草图不是最终结果。我只是画它来快速展示我想要实现的目标。此外,当我扩展 window 时,table 和标题的大小应该相应地增加。

提前致谢

如果你真的想使用 gird manager.You 可以使用 grid_size() 来获取行数和列数:

import tkinter as tk
# GUI
window = tk.Tk()
# set up window
height = window.winfo_screenheight()
width = window.winfo_screenwidth()
window.geometry(f'{int(width / 2)}x{int(height / 2)}')

frm_window = tk.Frame(window)
frm_window.pack(fill="x")

# title section
frm_title = tk.Frame(frm_window)
frm_title.grid(row=0, column=0, sticky="nsew")
lbl_title = tk.Label(master=frm_title, text="question")
lbl_title.pack()

# table section
frm_table = tk.Frame(frm_window)
frm_table.grid(row=1, column=0, sticky="nsew")
tbl_title = tk.Label(frm_table, text="subtitle", relief=tk.RAISED)
tbl_title.grid(row=0, column=0, columnspan=5, sticky="ew")

# row 1
l1 = ['item', 'item', 'item', 'item', 'item']
for j in range(0, len(l1)):
    name = l1[j]
    cell = tk.Label(frm_table, text=name, relief=tk.RAISED)
    cell.grid(row=1, column=j, sticky="nsew")

# row2
l2 = ['item', 'item', 'item', 'item', 'item']
for i in range(0, len(l2)):
    x = l2[i]
    cell2 = tk.Label(frm_table, text=x, relief=tk.RAISED)
    cell2.grid(row=2, column=i, sticky="nsew")

for i in range(frm_window.grid_size()[0]):
    frm_window.grid_columnconfigure(i, weight=1)

for i in range(frm_table.grid_size()[0]):
    frm_table.grid_columnconfigure(i, weight=1)

window.mainloop()