Oct-10-2023, 03:08 PM
(This post was last modified: Oct-10-2023, 04:08 PM by deanhystad.)
What makes you think the labels are not created? You don't supply any label text, so how could you tell?
When creating tkinter widgets you should pass the parent widget as the first argument.
I don't think you want to add attributes to global. Make a list of entries and a function for creating an entry. Do the same for labels. Better yet. make a window class that has a dictionary of labels and a dictionary of entries.
When creating tkinter widgets you should pass the parent widget as the first argument.
I don't think you want to add attributes to global. Make a list of entries and a function for creating an entry. Do the same for labels. Better yet. make a window class that has a dictionary of labels and a dictionary of entries.
import tkinter as tk class Window(tk.Tk): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.label = {} self.entry = {} def add_label(self, text, id=None, row=None, col=0, **kwargs): """Add entry to window.""" id = id or text if row is None: row = len(self.entry) self.label[id] = tk.Label(self, text=text, **kwargs) self.label[id].grid(row=row, column=col, padx=5, pady=5) return label[id] def add_entry(self, id, row=None, col=1, **kwargs): """Add entry to window.""" if row is None: row = len(self.entry) self.entry[id] = tk.Entry(self, **kwargs) self.entry[id].grid(row=row, column=col, padx=5, pady=5) return entry[id] def dump(): for label, entry in zip(window.label, window.entry.values()): print(f"{label:10} {entry.get()}") window = Window() window.add_label("Row 1") window.add_entry("Row 1") window.add_label("Row 2") window.add_entry("Row 2") button = tk.Button(window, text="Press Me", command=dump) button.grid(row=len(window.entry), column=0, columnspan=2, padx=5, pady=5) window.mainloop()