i have create automatic entry box
but values print dictionary
example {["ent1":'computer',"ent2": '1800'], ["ent1":'laptop', "ent2":'2000'], ["ent1":'mouse',"ent2": '500']}
from tkinter import *
class sample:
def __init__(self, root):
self.root = root
self.root.geometry("500x500")
self.all_entries = list()
showButton = Button(root, text='Show all text',command= self.showEntries)
showButton.pack()
addboxButton = Button(root, text='<Add Time Input>', fg="Red", command=self.addBox)
addboxButton.pack()
def addBox(self):
frame = Frame(root)
frame.pack()
self.ent1 = Entry(frame)
self.ent1.grid(row=1, column=0)
self.ent2 = Entry(frame)
self.ent2.grid(row=1, column=1)
self.all_entries.append([self.ent1,self.ent2])
def showEntries(self):
values = [[entry.get() for entry in entry_set] for entry_set in self.all_entries]
print(values)
root = Tk()
obj = sample(root)
root.mainloop()
CodePudding user response:
If you want to print in list of dictionary, change
values = [[entry.get() for entry in entry_set] for entry_set in self.all_entries]
to
values = [{f"ent{i}":entry.get() for i,entry in enumerate(entry_set,1)} for entry_set in self.all_entries]
The output of print(values)
will be something like:
[{'ent1': 'value1', 'ent2': 'value2'}, {'ent1': 'hello', 'ent2': 'world'}]