0

I have developed the following gui using grid in tkinter. I know there are other ways but grid is kind of more intuitive for me, consider I am a newbie with it. Here is the code

import tkinter as tk
from tkinter import ttk


def make_form(row_c, col_c, list_names, type_input):
    #  variable definition
    col_range = 2
    value = []  # metti lunghezza list names!
    for ind, key in enumerate(list_names):
        row_c += ind
        lab = ttk.Label(root, text=key)
        lab.grid(column=col_c, row=row_c, sticky=tk.W, padx=5, pady=5)
        if type_input == 'in':
            value = ttk.Entry(root)
            value.grid(column=col_c + col_range, row=row_c, sticky=tk.E, padx=5, pady=5)
            value = value.get()
        elif type_input == 'out':
            value = ttk.Label(root, text='test_value')
            value.grid(column=col_c + col_range, row=row_c, sticky=tk.E, padx=5, pady=5)
        else:
            pass  # gestire eventuali messaggi di errore

    return row_c, col_c + col_range, value


def make_buttons(row_button, col_button, label):
    var_button = ttk.Button(root, text=label)
    var_button.grid(column=col_button, row=row_button, sticky=tk.W, padx=5, pady=5)


if __name__ == "__main__":
    root = tk.Tk()
    # root.geometry("240x100")
    root.title('Titolo')
    root.resizable(1, 1)

    # configure the grid
    root.columnconfigure(0, weight=1)
    root.columnconfigure(1, weight=1)
    root.columnconfigure(2, weight=1)
    root.columnconfigure(3, weight=1)

    #  gui template
    c_title = 0  # columnspan=4
    c_input_1 = 0  # columnspan=2
    c_input_2 = 3  # columnspan 2
    c_labels_1 = 2  #
    c_labels_2 = 4
    c_file_path = 0
    c_button_file = 4

    r = 0
    label = ttk.Label(root, text='Name window', borderwidth=2, relief="groove")
    label.grid(row=r, column=c_title, columnspan=2, sticky=tk.S, padx=5, pady=5)

    r = 1
    label = ttk.Label(root, text='Input 1', borderwidth=2, relief="groove")
    label.grid(row=r, column=c_input_1, columnspan=2, sticky=tk.S, padx=5, pady=5)
    label = ttk.Label(root, text='Input 2', borderwidth=2, relief="groove")
    label.grid(row=r, column=c_input_2, columnspan=2, sticky=tk.S, padx=5, pady=5)

    # input section
    r = 2
    list_labels = ['var_1', 'var_2', 'var_3', 'var_4', 'var_5']
    [row_1, col_1, values_1] = make_form(r, 0, list_labels, 'in')
    list_labels = ['var_1', 'var_2', 'var3', 'var_4', 'var_5', 'var_6', 'var_7']
    [row_2, col_2, values_2] = make_form(r, col_1 + 1, list_labels, 'in')

    #  input file section
    c = 0
    label = ttk.Label(root, text='Name window', borderwidth=2, relief="groove")
    label.grid(row=row_2 + 1, column=c, columnspan=3, sticky=tk.N, padx=5, pady=5)
    c = 4
    var = ttk.Entry(root)
    var.grid(row=row_2 + 2, column=4, columnspan=1, sticky=tk.W, padx=5, pady=5)
    var = var.get()

    B = tk.Button(root, text="Hello")
    B.grid(row=row_2 + 2, column=4, sticky=tk.E, padx=5, pady=5)

    r = row_2 + 3
    label = ttk.Label(root, text='output 1', borderwidth=2, relief="groove")
    label.grid(row=r, column=2, columnspan=2, sticky=tk.S, padx=5, pady=5)

    label = ttk.Label(root, text='output 2', borderwidth=2, relief="groove")
    label.grid(row=r, column=4, columnspan=2, sticky=tk.S, padx=5, pady=5)

    #  display output values
    # input section
    list_labels = ['var_1', 'var_2', 'var_3', 'var_4', 'var_5']
    [row_1, col_1, values_1] = make_form(r + 1, 0, list_labels, 'out')

    list_labels = ['var_1', 'var_2', 'var3', 'var_4', 'var_5', 'var_6', 'var_7']
    [row_2, col_2, values_2] = make_form(r + 1, col_1 + 1, list_labels, 'out')

    root.mainloop()

I found a valuable information here tkinter gui layout using frames and grid, but I am still struggling with my layout. I also have the following questions: -why does the "Name window" not span through the whole gui? can i do it?

Henry
  • 3,472
  • 2
  • 12
  • 36
drSlump
  • 33
  • 1
  • 6

1 Answers1

1

why does the "Name window" not span through the whole gui? can i do it?

Because you haven't told it to span the whole window. Your window has six columns but you've configured them to span either two or three columns.

If you want them to use the whole window in the x direction, you need them to span all columns. If you want the labels to stretch to fit, you will also need to use the proper sticky attributes.

label.grid(row=r, column=c_title, columnspan=6, sticky="ew", padx=5, pady=5)
...
label.grid(row=row_2 + 1, column=c_title, columnspan=6, sticky="ew", padx=5, pady=5)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685