Add multiple Entries and retrieve data from them dynamically

非 Y 不嫁゛ 提交于 2019-12-11 08:09:52

问题


Is there any way to add multiple Entries dynamically and get values from them?

Like in the example below if I click Show Data button all data is shown in different Entries. Now if I change data in any of the Entries how can I get the updated data and save them to Values array?

from tkinter import *
from tkinter import ttk

class Root(Tk):

    def __init__(self):
        super(Root, self).__init__()
        self.createMultipleEntries()

    def createMultipleEntries(self):
        self.values = [
            ["Full Name", "Email", "Phone", "Street Address", "Job Title"],
            ["Alex Blackater", "ablackater0@wordpress.org", "507-340-8136", "81604 Moose Park", "Senior Financial Analyst"],
            ["Alvy Chartres", "achartres1@linkedin.com", "926-801-1685", "4 Sundown Place", "Executive Secretary"],
            ["Juieta Train", "jtrain2@ucoz.com", "861-881-9312", "953 Hintze Circle", "Professor"],
            ["Basil Effemy", "beffemy3@liveinternet.ru", "156-293-8817", "115 Brentwood Trail", "Research Associate"],
            ["Asher MacAllister", "amacallister4@t-online.de", "263-605-8166", "8672 Stoughton Road", "Software Engineer II"]
        ]

        self.label_frame = ttk.LabelFrame(self)
        self.label_frame.pack()

        self.button_show = ttk.Button(self.label_frame, text="Show Data", command=self.onShowButtonClick)
        self.button_show.grid(row=0, column=0)
        self.button_update = ttk.Button(self.label_frame, text="Update Data", command=self.onUpdateButtonClick)
        self.button_update.grid(row=0, column=1)

    def onShowButtonClick(self):
        self.values_length = len(self.values)
        for i in range(1, self.values_length):
            entry_full_name = ttk.Entry(self.label_frame)
            entry_full_name.grid(row=i, column=0)
            entry_full_name.insert(0, self.values[i][0])

            entry_email = ttk.Entry(self.label_frame)
            entry_email.grid(row=i, column=1)
            entry_email.insert(0, self.values[i][1])

            entry_phone = ttk.Entry(self.label_frame)
            entry_phone.grid(row=i, column=2)
            entry_phone.insert(0, self.values[i][2])

            entry_street_address = ttk.Entry(self.label_frame)
            entry_street_address.grid(row=i, column=3)
            entry_street_address.insert(0, self.values[i][3])

            entry_job_title = ttk.Entry(self.label_frame)
            entry_job_title.grid(row=i, column=4)
            entry_job_title.insert(0, self.values[i][4])

    def onUpdateButtonClick(self):
        pass

def main():
    root = Root()
    root.mainloop()

if __name__ == '__main__':
    main()

Is there any way to set something like below dynamically?

entery_variable = StringVar()

entry_ = ttk.Entry(self.label_frame, textvariable=entery_variable)
entry_.grid(row=i, column=0)

print(entry_variable.get())

Thanks in advance. :)


回答1:


Regarding updating your values array, here is what I came up with.

I stored all the entries in a list, so that I can refer to it by its 2D index (i,j). When you click the update button, it gets the values from all the entries and updates the values array. Here is the code.

from tkinter import *
from tkinter import ttk

class Root(Tk):

    def __init__(self):
        super(Root, self).__init__()
        self.createMultipleEntries()

    def createMultipleEntries(self):
        self.values = [
            ["Full Name", "Email", "Phone", "Street Address", "Job Title"],
            ["Alex Blackater", "ablackater0@wordpress.org", "507-340-8136", "81604 Moose Park", "Senior Financial Analyst"],
            ["Alvy Chartres", "achartres1@linkedin.com", "926-801-1685", "4 Sundown Place", "Executive Secretary"],
            ["Juieta Train", "jtrain2@ucoz.com", "861-881-9312", "953 Hintze Circle", "Professor"],
            ["Basil Effemy", "beffemy3@liveinternet.ru", "156-293-8817", "115 Brentwood Trail", "Research Associate"],
            ["Asher MacAllister", "amacallister4@t-online.de", "263-605-8166", "8672 Stoughton Road", "Software Engineer II"]
        ]

        self.label_frame = ttk.LabelFrame(self)
        self.label_frame.pack()

        self.button_show = ttk.Button(self.label_frame, text="Show Data", command=self.onShowButtonClick)
        self.button_show.grid(row=0, column=0)
        self.button_update = ttk.Button(self.label_frame, text="Update Data", command=self.onUpdateButtonClick)
        self.button_update.grid(row=0, column=1)

        self.entryList = []

    def onShowButtonClick(self):
        self.values_length = len(self.values)
        for i in range(1, self.values_length):
            entry_full_name = ttk.Entry(self.label_frame)
            entry_full_name.grid(row=i, column=0)
            entry_full_name.insert(0, self.values[i][0])

            entry_email = ttk.Entry(self.label_frame)
            entry_email.grid(row=i, column=1)
            entry_email.insert(0, self.values[i][1])

            entry_phone = ttk.Entry(self.label_frame)
            entry_phone.grid(row=i, column=2)
            entry_phone.insert(0, self.values[i][2])

            entry_street_address = ttk.Entry(self.label_frame)
            entry_street_address.grid(row=i, column=3)
            entry_street_address.insert(0, self.values[i][3])

            entry_job_title = ttk.Entry(self.label_frame)
            entry_job_title.grid(row=i, column=4)
            entry_job_title.insert(0, self.values[i][4])

            self.entryList.append([entry_full_name, entry_email, entry_phone, entry_street_address, entry_job_title])

    def onUpdateButtonClick(self):
        for i in range(1, len(self.values)):
            for j in range(len(self.values[0])):
                self.values[i][j] = self.entryList[i-1][j].get()
        # print(self.values)

def main():
    root = Root()
    root.mainloop()

if __name__ == '__main__':
    main()

P.S. I would really like to know though, if what I did is an overkill !




回答2:


I'm not clear as to exactly how you woult like it to function, but here is an idea: bind each entry to a function which reacts to Return and do your processing in the callback function.

def onShowButtonClick(self):
    self.values_length = len(self.values)
    for i in range(1, self.values_length):

        # creating the entrys...

        entry_full_name.bind('<Return>', self.change)
        entry_email.bind('<Return>', self.change)
        entry_phone.bind('<Return>', self.change)
        entry_street_address.bind('<Return>', self.change)
        entry_job_title.bind('<Return>', self.change)

def change(self, event):
    print(event.widget.get())

If you rather the entry should catch the actual changing of each entry you should associate them to a StringVar() at creation and then use trace to hook any alterations to the entry text.



来源:https://stackoverflow.com/questions/53359527/add-multiple-entries-and-retrieve-data-from-them-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!