Python Forum

Full Version: Beginner question re: Tkinter geometry
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to create a data entry form that takes user input in categories and displays it in a grid.

The code below illustrates what I am trying to accomplish. I am collecting data in categories A, B, and C, which is then displayed in a grid.

However, because the amount of input may vary (i.e., each category A, B, C may have any number of entries), there is no way to assign grid locations to the categories in advance.

Is there any way to assign grid or place locations as variables that receive their value as a function of the number of entries in each category?

Any help is greatly appreciated!

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

A = [1, 2, 3]
B = [4, 5, 6]
C = [7, 8, 9]
D = ['A', 'B', 'C']

for i in range(len(D)):
     headings = ttk.Label(root, text = (D[i]+': '), font = 'verdana 12 bold').pack()
     while D[i] == 'A':
          for j in range(len(A)):
               CatA = ttk.Label(root, text = A[j]).pack()
          break
     while D[i] == 'B':
          for j in range(len(B)):
               CatB = ttk.Label(root, text = B[j]).pack()
          break
     while D[i] == 'C':
          for j in range(len(C)):
               CatC = ttk.Label(root, text = C[j]).pack()
          break

root.mainloop()
import tkinter as tk

class MyWindow(tk.Tk):
    """A window with arbitrary content"""
    def __init__(self, rows):
        super().__init__()
        for r, row in enumerate(rows):
            for c, col in enumerate(row):
                tk.Button(self, text=col).grid(row=r, column=c, ipadx=10, ipady=2)


ragged_table = ('ABC', 'DEFG', 'HI')
MyWindow(ragged_table).mainloop()
This is a job for a Listbox
import tkinter as tk

def listbox_insert(lb, items):
    for var in items:
        lb.insert("end", var)

list_a = [1, 2, 3]
list_b = [4, 5, 6, 'x', 'y']
list_c = [7, 8, 9, 'z']
D = ['A', 'B', 'C']

root = tk.Tk()
 
listb_a = tk.Listbox(root, height=6, width=20, font=('Fixed', 14) )
listb_a.grid(row=0, column=0)
listbox_insert(listb_a, list_a)

listb_b = tk.Listbox(root, height=6, width=20, font=('Fixed', 14) )
listb_b.grid(row=0, column=1)
listbox_insert(listb_b, list_b)

listb_c = tk.Listbox(root, height=6, width=20, font=('Fixed', 14) )
listb_c.grid(row=0, column=2)
listbox_insert(listb_c, list_c)

root.mainloop()
Listbox or combobox are great choices if the OP is asking how you can choose a value from multiple options (select 1, 2 or 3 for field A). Listbox provides no help if the OP is asking how you can make a generic form that will format itself to an arbitrary number of fields. I'm interested to hear what the real question is.