按下按钮时如何清除列表框条目?

How to clear listbox entries when pressing a button?

我正在使用列表框,以便在按下按钮时显示一些文本。但是,当我再次按下此按钮重新显示时,它只是将文本放在 listbox 中原始文本的下方。我已经寻找解决此问题的方法,但似乎找不到适合我的特定程序的方法。我的程序的重点是检查用户输入的密码。

这是我的代码:

#!/usr/bin/env python

#Imports
from tkinter import *
from random import *
import string

#Root And GUI Title
root = Tk()
root.wm_title("Password Checker")

#Top Frame
topFrame = Frame(root)
topFrame.grid(row=0)

#Bottom Frame And Geometry And Check Labels
bottomFrame = Frame(root)
bottomFrame.grid(row=10)
root.geometry("1000x1000")
checkLabel = Label(root)
checkCap = Label(root)
checkLow = Label(root)
checkSymb = Label(root)
checkDig = Label(root)

listbox = Listbox(root)
listbox.grid(row=6, column=1)


#Checker Function 
def checker():
    if len(passEntry.get()) < 8 or len(passEntry.get()) > 24:
        listbox.insert(END, "Invalid Length")
        listbox.grid(row=1, column=1)

    if len(passEntry.get()) >= 8 and len(passEntry.get()) <= 24:
        #checkLabel.config(text="Valid Length")
        #checkLabel.grid(row=10, column = 1)
        listbox.insert(END, "Valid Length")


    cap = re.search("[A-Z]", passEntry.get())
    if cap:
        #checkCap.config(text="Okay Capitals")
        #checkCap.grid(row=11, column = 1)
        listbox.insert(END, "Okay Capitals")
    else:
        #checkCap.config(text="No Capitals")
        #checkCap.grid(row=11, column = 1)
        listbox.insert(END, "No Capitals")
    low = re.search("[a-z]", passEntry.get())
    if low:
        #checkLow.config(text="Okay Lowercase")
        #checkLow.grid(row=12, column = 1)
        listbox.insert(END, "Okay Lowercase")
    else:
        #checkLow.config(text="No Lowercase")
        #checkLow.grid(row=12, column = 1)
        listbox.insert(END, "No Lowercase")
    symb = re.search("[!£$%^&*()]", passEntry.get())
    if symb:
        #checkSymb.config(text="Okay Symbols")
        #checkSymb.grid(row=13, column= 1)
        listbox.insert(END, "Okay Symbols")
    else:
        #checkSymb.config(text="No Symbols")
        #checkSymb.grid(row=13, column = 1)
        listbox.insert(END, "No Symbols")
    dig = re.search("[0-9]", passEntry.get())
    if dig:
        #checkDig.config(text="Okay Digits")
        #checkDig.grid(row=14, column = 1)
        listbox.insert(END, "Okay Digits")
    else:
        #checkDig.config(text="No Digits")
        #checkDig.grid(row=14, column = 1)
        global noDigits
        noDigits = listbox.insert(END, "No Digits")


#Password Entry
passEntryLabel = Label(root, text="Password")
passEntryLabel.grid(row=0, column=3)
passEntry = Entry(root)
PAR = passEntry.get()
passEntry.grid(row=0, column=4)
checkButton = Button(root, text="Check Password", command=checker)
checkButton.grid(row=0, column=7)
#Mainloop
root.mainloop()

当我输入第一个密码时: First Password Entry

当我输入第二个密码时: Second Password Entry

输入第二个密码后,它只是将下一组检查放在旧检查的下面,那么我如何才能在输入第二个密码时删除旧检查?谢谢

只需添加 documented:

listbox.delete('0', 'end')

delete all entries in listbox to, -- the method that updates your Listboxchecker为第一行。