[Python] Why entry fields of row and column are streching when i generete grid

Discussão em 'Python' iniciado por Stack, Janeiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I am building a simple application in Python using Tkinter where the user can specify the number of rows and columns they want in a grid. Based on the input, a grid of Entry widgets is dynamically generated. However, I am encountering an issue where the Entry widget for column input (used to specify the number of columns) stretches horizontally whenever the number of columns increases.

    matrixA=tk.Label(matrix,text="Matrix A Input")
    matrixA.grid(row=0,column=2)
    #Row Input field
    row=tk.Label(matrix,text="Row")
    row.grid(row=1,column=0)
    row_num = tk.Entry(matrix,width=5)
    row_num.grid(row=1,column=1,sticky='nw')


    # Column Input field
    col=tk.Label(matrix,text="Column")
    col.grid(row=1,column=2)
    col_num=tk.Entry(matrix,width=5)
    col_num.grid(row=1,column=3,sticky='nw')


    grid_frame=tk.Frame(matrix)
    grid_frame.grid(row=3,column=2,pady=10)
    grid_entries = {}
    def generate_grid(event=None):
    rows_input=row_num.get().strip()
    cols_input=col_num.get().strip()
    if not rows_input.isdigit() or not cols_input.isdigit():
    messagebox.showwarning("Warning", "Input Valid Digit")
    rows=int(rows_input)
    cols=int(cols_input)
    for widget in grid_frame.winfo_children():
    widget.destroy()
    for r in range(rows):
    for c in range(cols):
    grid=tk.Entry(grid_frame,width=5)
    grid.grid(row=r,column=c,padx=5,pady=5)
    grid_entries[(r,c)]=grid
    root.bind('<Return>',generate_grid)

    Continue reading...

Compartilhe esta Página