Python Forum
Beginner question re: Tkinter geometry
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner question re: Tkinter geometry
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()
     while D[i] == 'B':
          for j in range(len(B)):
               CatB = ttk.Label(root, text = B[j]).pack()
     while D[i] == 'C':
          for j in range(len(C)):
               CatC = ttk.Label(root, text = C[j]).pack()

import tkinter as tk

class MyWindow(tk.Tk):
    """A window with arbitrary content"""
    def __init__(self, rows):
        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')
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)

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.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter GUI question texan1836 3 1,675 Apr-13-2023, 03:12 AM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,193 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] question for a tkinter dialog box RobertAlvarez424 2 2,160 Aug-25-2021, 03:08 PM
Last Post: RobertAlvarez424
  Python tkinter question tablet Nick_tkinter 8 4,799 Mar-04-2021, 10:44 PM
Last Post: Larz60+
  tkinter slider question Nick_tkinter 1 2,367 Feb-22-2021, 01:31 PM
Last Post: deanhystad
Question [Tkinter] How to change geometry? Meffy 4 2,763 Jan-31-2021, 10:35 AM
Last Post: Meffy
  [Tkinter] Noob question:Using pyttsx3 with tkinter causes program to stop and close, any ideas? Osman_P 4 5,138 Nov-14-2020, 10:51 AM
Last Post: Osman_P
  question on tkinter canvas PhotoImage gr3yali3n 1 2,076 Sep-05-2020, 12:18 PM
Last Post: Larz60+
  Tkinter parameter question DPaul 2 1,979 Mar-14-2020, 09:35 AM
Last Post: DPaul
  [Tkinter] Window geometry appears different on Win and Linux steve_shambles 6 6,759 Nov-29-2019, 12:30 AM
Last Post: steve_shambles

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020