【问题标题】:Reading into an array but every time i read into the array it rewrites over it and only prints the last entered value读入一个数组,但每次我读入数组时,它都会重写它并且只打印最后输入的值
【发布时间】:2022-01-25 00:44:00
【问题描述】:

我是编码新手,我正在尝试从文本框中读取数组,但每次输入新值时,该值都会设置为 0 并覆盖上次输入的值。我指的是def提交函数

from tkinter import *


def submit():
    
    data=[]
    while True:
        entry = str(textBox.get())
        print("*"+entry+"*")
        data.append(entry)
        print("#"+entry+"#")
        textBox.delete(0,END)
        print(data)
        if entry == "":
            print(data) 
        break
    
        
gui1 = Tk()
gui1.title("Main Screen")
gui1.geometry("800x500")
gui1.config(bg="black")

submit = Button(gui1,text="submit",command=submit) 
submit.pack(side = BOTTOM, padx=5,pady = 5)

label = Label(gui1, text = "Enter name: ")
label.config(font=("Consolas",15))
label.pack(side=LEFT,padx=50, pady=50)

textBox = Entry()
textBox.config(font=('Ink Free',15)) #change font
textBox.config(bg="White") #background
textBox.config(fg="Red") #foreground
textBox.config(width=50) #width displayed in characters
#textBox.insert(0,'Spongebob') #set default text
#textBox.config(state=DISABLED) #ACTIVE/DISABLED
#textBox.config(show='*') #replace characters shown with x character
textBox.pack(side= LEFT,padx=25,pady=25)


gui1.mainloop() 

【问题讨论】:

    标签: python spyder


    【解决方案1】:

    因为你已经在submit函数里面初始化了data,只要在submit函数上面初始化就可以得到想要的输出了

    data=[]
    def submit():
      
        while True:
            entry = str(textBox.get())
            print("*"+entry+"*")
            data.append(entry)
            print("#"+entry+"#")
            textBox.delete(0,END)
            print(data)
            if entry == "":
                print(data) 
            break
    

    Output can be seen by clicking the link

    【讨论】:

    • 非常感谢,非常感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2021-09-04
    • 2015-01-17
    • 2015-01-23
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多