add entries and labels to the window tkinter - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: add entries and labels to the window tkinter (/thread-40895.html) |
add entries and labels to the window tkinter - jacksfrustration - Oct-10-2023 Basically i want to build an app where the user can add entries and labels to the window i have the following code global cur_row,count cur_row+=1 globals()[f'lbl{count}']=Label() globals()[f"lbl{count}"].grid(row=cur_row,column=0) globals()[f"ent{count}"]=Entry() globals()[f"ent{count}"].grid(row=cur_row,column=1) count+=1the problem is the labels are not generated. All i get is the entry. I want a way to generate labels as well as entries. after that i need a way to retrieve all of the entry inputs and save them to a dictionary or csv file RE: add entries and labels to the window tkinter - deanhystad - Oct-10-2023 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. 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() RE: add entries and labels to the window tkinter - jacksfrustration - Oct-10-2023 ok forget this reply. i found the solution RE: add entries and labels to the window tkinter - buran - Oct-10-2023 Modifyng dict returned from globals() for sole purpose to dynamically create names is badWhy you don't want to dynamically create variables |