小能豆

添加多个条目并从中动态检索数据

py

有没有办法动态添加多个条目并从中获取值?

如下例所示,如果我单击“显示数据”按钮,则所有数据都会显示在不同的条目中。现在,如果我更改任何条目中的数据,如何获取更新的数据并将其保存到 Values 数组中?

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()

有什么方法可以动态设置下面的内容吗?

entery_variable = StringVar()

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

print(entry_variable.get())

阅读 16

收藏
2025-01-07

共1个答案

小能豆

关于更新您的值数组,这是我的想法。

我将所有条目存储在一个列表中,以便我可以通过其二维索引 (i,j) 引用它。当您单击更新按钮时,它会从所有条目中获取值并更新值数组。以下是代码。

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()

PS:我真的很想知道我所做的是否有点过头了!

2025-01-07