有没有办法在 tkinter 中获取动态添加的文本字段的值?

Is there a way to get values of dynamically added text fields in tkinter?

作为家庭作业,我必须创建一个应用程序,每次用户单击按钮时都会创建一个文本字段,然后在按下 "submit" 按钮时从字段中获取值。 trace方法反复出现,不知道怎么用。我知道它需要一个回调函数,但是那个回调函数应该是什么?

from tkinter import *
from tkinter import ttk
import sqlite3
import getpass
import wipComingIn

class Application(object):
    def __init__(self,master):
        self.master=master
        self.ScanWIPIn = Button(master, text="Scan WIP In", width=25,         
font='Calibri 12 
bold',background='snow',command=self.scanWIPIn).grid(row=0, column=0, 
padx=10)

def scanWIPIn(self):
    incomingInventory=wipComingIn.scanIn()

def main():
    root = Tk()
    app=Application(root)
    root.title("Main Menu")
    root.configure(background="light cyan")
    root.resizable(0, 0)
    root.geometry('230x230+300+80')
    root.mainloop()

if __name__=='__main__':
    main()

class scanIn(Toplevel):

    def __init__(self):
        Toplevel.__init__(self)
        self.geometry('300x100+350+100')
        self.title('Scan In')
        self.resizable(0,0)
        self.num_rows=1
        self.LocationLb = Label(self,text='Scan Location:',font='Arial 
12').grid(row=1,column=1)
        self.LocationBCText = Entry(self).grid(row=1,column=2)
        self.AddLotBtn= Button(self,text="Scan 
Lot",command=self.addField).grid(row=2,column=1)
        self.CompleteTransaction = 
Button(self,text="Complete",command=self.AddEntry).grid(row=2,column=4)

    global listOfLots
    listOfLots=[]
    listOfLocation=[]
    global rowNum
    rowNum=2

def addField(self):
    height =Toplevel.winfo_height(self)
    height=height+25
    global rowNum
    rowNum=rowNum+1
    listOfLots.append(StringVar())

    newLot = Entry(self, textvariable=listOfLots[rowNum - 2])

    newLot.grid(row=rowNum,column=2, pady=1)
    listOfLots.append(StringVar())
    geometryText='300'+str(height)+'350+100'
    print(geometryText)
    self.geometry('300x'+str(height)+'+350+100')
    newLot.focus_set()

您可以尝试制作一个 class 来实现它,例如:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.geometry('200x200')

class EntryListWidget(ttk.Frame):
    """Widget that creates a column of entry boxes."""
    def __init__(self, master):
        super().__init__(master)
        self.entries = []

    def add_entry(self):
        """Creates a new entry box and keeps reference to respective variable."""
        entry_var = tk.StringVar()
        self.entries.append(entry_var)
        ttk.Entry(self, textvariable=entry_var).pack()

    def get_entries(self):
        """Gets each entrybox text and returns as list."""
        return [entry.get() for entry in self.entries]

entry_widget = EntryListWidget(root)
entry_widget.pack()

# Buttons to control adding new entry and getting their values
ttk.Button(root, text='Add Entry', command=entry_widget.add_entry).pack()
ttk.Button(root, text='Get Entries', command=entry_widget.get_entries).pack()

root.mainloop()

只使用变量classes,不跟踪;实际上我不会在这种情况下使用 trace,因为我相信每次变量更改时 trace 都会使用回调,这里您有一个收集所有值的一次性 "submit" 按钮。你可以扩展这个 class 的想法来得到你想要的东西,我敢打赌。