Python 正在努力布局我的Tkinter应用程序

Python 正在努力布局我的Tkinter应用程序,python,tkinter,tkinter-layout,Python,Tkinter,Tkinter Layout,我需要帮助构建我的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 =

我需要帮助构建我的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()
目前,这是输出:

我计划在表下添加更多的小部件,这样草图就不是最终结果了。我只是画了它来快速展示我想要达到的目标。此外,当我展开窗口时,表和标题的大小也会相应增大


如果您确实想使用
gird
manager,请提前感谢您。您可以使用
grid\u 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()