在输入数据后,如何在以下代码中添加代码以清除输入字段?

时间:2016-09-05 14:37:17

标签: python tkinter

我进入这个想法它可能有效,但它没有。不确定这是否正确或我需要更改它。它没有用,所以我猜它确实需要改变。

从tkinter导入:

from tkinter import * 
import csv
def delete_entries():
    for field in fields:
        field.delete(0,END)

class App(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.output()

输出:

def output(self):
    Heading=StringVar()
    Heading.set("Please enter student name below")
    Label(text='Name:').pack(side=LEFT,padx=5,pady=5)
    self.n = Entry(root, width=10)
    self.n.pack(side=LEFT,padx=5,pady=5)

文字标签:

    Label(text='Grade:').pack(side=LEFT,padx=5,pady=5)
    self.e = Entry(root, width=10)
    self.e.pack(side=LEFT,padx=6,pady=6)

    self.b = Button(root, text='Submit', command=self.writeToFile)
    self.b.pack(side=RIGHT,padx=5,pady=5)

    self.b = Button(root, text='Clear', command=self.writeToFile)
    self.b.pack(side=RIGHT,padx=5,pady=5)

写入等级CSV:

def writeToFile(self):
    with open('Grades.csv', 'a') as f:
        w=csv.writer(f, quoting=csv.QUOTE_ALL)
        w.writerow([self.n.get()])
        w.writerow([self.e.get()])

if __name__ == "__main__":
    root=Tk()
    root.title('grade')
    root.geometry('380x280')
    app=App(master=root)
    app.mainloop()
    root.mainloop()

    Delete_button = Button(root, text = 'Clear', command = delete_entries)
    Delete_button.pack()

1 个答案:

答案 0 :(得分:0)

您可以将其添加到提交条目的代码的末尾

self.n.delete(0, END)
self.e.delete(0, END)