Python Forum
add entries and labels to the window tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
add entries and labels to the window tkinter
#1
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+=1
the 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
Reply
#2
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()
Reply
#3
ok forget this reply. i found the solution
Reply
#4
Modifyng dict returned from globals() for sole purpose to dynamically create names is bad
Why you don't want to dynamically create variables
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  wrong entries in sqlite database and tkinter epsilondatum 2 349 Apr-23-2024, 04:48 PM
Last Post: epsilondatum
  Is there a way to call and focus any popup window outside of the main window app? Valjean 6 1,804 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  how to open a popup window in tkinter with entry,label and button lunacy90 1 902 Sep-01-2023, 12:07 AM
Last Post: lunacy90
Bug tkinter.TclError: bad window path name "!button" V1ber 2 806 Aug-14-2023, 02:46 PM
Last Post: V1ber
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,185 Jun-13-2022, 08:59 AM
Last Post: Shena76
  Closing Threads and the chrome window it spawned from Tkinter close button law 0 1,717 Jan-08-2022, 12:13 PM
Last Post: law

Forum Jump:

User Panel Messages

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