Search code examples
pythontkintertkinter-layout

Struggling to layout my Tkinter application


I need help with structuring my Tkinter program to look like this simple drawing of the structure I want

This is my code:

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()

Currently , this is the output: enter image description here

I plan on adding more widgets under the table so the sketch is not the final result. I just drew it to show quickly what I'm trying to achieve. Also, when I expand the window, the table and title should grow in size accrodingly.

Thank you in advance


Solution

  • If you really want to use gird manager.You could use grid_size() to get the number of rows and columns:

    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()
    

    enter image description here