Python-tkinter: For 循环应该从数组中收集条目并传递给文本框,只传递最后一个条目

Python-tkinter: For loop supposed to collect entries from an array and pass to text box, only passes last entry

我是编码新手,如果这个问题有点基础,我深表歉意。无论如何,我在 python 制作的这个 GUI 应该收集几个用户条目,并将它们传递到 GUI 中的文本框。不过现在,代码末尾的 for 循环只是将最后一个条目放入文本框中。为什么?

from Tkinter import *

class Application(Frame):
""" A SPC program that takes user input and saves the file """
def __init__(self,master):
    """ initializes the frame """
    Frame.__init__(self,master)
    self.grid()
    #creates an array to store entries into
    self.entry_list = [None]*7
    self.create_widgets()


def create_widgets(self):
    """create widgets for user inputted data"""
    # creates a text widget next to the entries that displays what the user has input
    Label(self, text = "Do these values you entered seem correct?").grid(row = 0, column = 4, sticky = W, padx = 5, pady = 5)
    self.checktext = Text(self, width =15, height = 42, wrap = WORD)
    self.checktext.grid(row = 1, rowspan = 10, column = 4, sticky = W, padx =5, pady =5)

    # get name
    Label(self, text = "First Name:").grid(row = 0, column = 0, sticky = W, padx=5, pady=5)
    self.entry_list[0] = Entry(self)
    self.entry_list[0].grid(row = 0, column = 1)       
    Label(self, text = "Last Name:").grid(row = 1, column = 0, sticky = W, padx=5, pady=5)
    self.entry_list[1] = Entry(self)
    self.entry_list[1].grid(row = 1, column = 1)

    # get work order
    Label(self, text = "Work Order Number:").grid(row = 2, column = 0, sticky = W, padx=5, pady=5)
    self.entry_list[2] = Entry(self)
    self.entry_list[2].grid(row = 2, column = 1)

    # get todays date
    Label(self, text = "Todays Date:").grid(row = 3, column = 0, sticky = W, padx=5, pady=5)
    self.entry_list[3] = Entry(self)
    self.entry_list[3].grid(row = 3, column = 1)

    # get bubble number
    Label(self, text = "Bubble Number:").grid(row = 4, column = 0, sticky = W, padx=5, pady=5)
    self.entry_list[4] = Entry(self)
    self.entry_list[4].grid(row = 4, column = 1)

    # get USL and LSL
    Label(self, text = "USL:").grid(row = 5, column = 0, sticky = W, padx=5, pady=5)        
    self.entry_list[5] = Entry(self)
    self.entry_list[5].grid(row = 5, column = 1)    
    Label(self, text = "LSL:").grid(row = 6, column = 0, sticky = W, padx=5, pady=5)       
    self.entry_list[6] = Entry(self)
    self.entry_list[6].grid(row = 6, column = 1)                      

    # button to submit user entered values up to the input data values portion of the gui
    self.button = Button(self)
    self.button["text"] = "Submit"
    self.button["command"] = self.submit
    self.button.grid(row = 5, column = 2, sticky = W, padx=5, pady=5)

    # creates a spot to dictate whether USL and LSL are correct
    self.checklimits = Text(self, width = 20, height = 3, wrap = WORD)
    self.checklimits.grid(row = 6, column = 3, sticky = W, padx = 5)

    """# get User Input Data values
    Label(self, text = "Enter Results:").grid(row = 7, column = 0, sticky = W, padx=5, pady=5)        
    self.entry_list8 = Text(self, width = 15, height = 30)
    self.entry_list8.grid(row = 7, column = 1) """  

def submit(self):
    """ submits user data up to input data section of GUI and checks USL vs LSL"""
    # checks to make sure limits were imput properly
    USL = int(self.entry_list[5].get())
    LSL = int(self.entry_list[6].get())

    if USL > LSL:
        message = "Limits are good"
    else:
        message = "USL can't be less than LSL, please re-enter USL and LSL"

    self.checklimits.delete(0.0, END)
    self.checklimits.insert(0.0, message)


    # puts entries into text box
    for x in self.entry_list:
       entry = x.get()
       if entry:
            self.checktext.delete(0.0, END)
            self.checktext.insert(END, entry + "\n")




root = Tk()
root.title("SPC Input Program")
root.geometry("700x750")

app = Application(root)

问题在这里:

for x in self.entry_list:
   entry = x.get()
   if entry:
        self.checktext.delete(0.0, END)
        self.checktext.insert(END, entry + "\n")

您每次都在将下一个字符串放入小部件之前删除小部件中的所有内容。要解决此问题,只需不要每次都删除所有内容。如果您想在重新填充之前清除小部件,请将删除移出循环:

self.checktext.delete(0.0, END)
for x in self.entry_list:
   entry = x.get()
   if entry:
        self.checktext.insert(END, entry + "\n")

好吧,我想通了,天哪,我觉得自己很蠢。所以这是我之前的 for 循环:

for x in self.entry_list:
   entry = x.get()
   if entry:
        self.checktext.delete(0.0, END)
        self.checktext.insert(END, entry + "\n")

self.chectext.delete(0.0, END) 行用于删除用户每次提交时在文本框中输入的任何内容,但是通过在 for 循环内,它在添加下一个之前删除了每个条目。所以 duh,当然它只会显示最后一个条目,因为它删除了前几个!所以我把这条线放在 for 循环之外,像这样:

# puts entries into text box
    self.checktext.delete(0.0, END)     
    for x in self.entry_list:
       entry = x.get()
       if entry:
            print entry                
            self.checktext.insert(END, entry + "\n")

现在一切正常