销毁tkinter标签

2024-04-25 12:27:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我真的遇到了一个问题。 这是我的代码,我想做一个猜字游戏:

from random_words import RandomWords
t = Tk()
rw = RandomWords()
word = rw.random_word()
tt = "*" * len(word)
attempts = len(word) - len(word) // 3
l1 = Label(t,text="This Is Word Guessing Game.\nThere Will Be A Secret Word And You Will Try Every Time To Guess A Letter In It Till The Word Is Completely Guessed.\nGood Luck!")
l2 = Label(t,text=f"\nYou Should Enter One Letter In The Lower Case.\nAnyways,Only The First Letter Of Your Input Will Be Taken In The Lower Case.\nThe Word Consists Of {len(word)} Characters")
l1.grid(row=0, column=0)
l2.grid(row=1, column=0)
def onclick(event):
    if guessi.get() == "Enter Your Guess: ":
        guessi.delete(0, END)
def end():
    b2.destroy()
    guessi.destroy()
    Label(t, text=f"\nGame Over\n{wol}").grid(row=5, column=0)
    Label(t, text=f"\nThe Correct Word Is: {word}").grid(row=6, column=0)
def Try():
    global guess, attempts, tt
    statement = StringVar()
    hh = guessi.get()
    guess = str(hh)
    if guess != "" and guess != "Enter Your Guess: ":
        guess = guess[0].lower()
        if guess in word:
            r = "Correct Guess!"
            pos = word.index(guess)
            if guess in tt:
                if word.count(guess) < (tt + guess).count(guess):
                    r = "Wrong guess"
                    attempts -= 1
                else:
                    pos = word.index(guess, pos + 1)
            tt = tt[:pos] + guess + tt[pos + 1:]
            statement.set(f"\n{r}\nThe Current Word Is: {tt}")
        else:
            r = "Wrong Guess"
            statement.set(f"\n{r}\nThe Current Word Is: {tt}")
            attempts -= 1
        global ll1, l3
        ll1 = Label(t, textvariable=statement)
        ll1.grid(row=4, column=0)
        ar = StringVar()
        ar.set(f"Attempts Remaining: {attempts}")
        l3 = Label(t, textvariable=ar)
        l3.grid(row=0, column=0)
        global wol
        if attempts == 0:
            wol = "You Lost"
            end()
        if tt == word:
            wol = "You Won"
            end()
def start():
    l1.destroy()
    l2.destroy()
    b1.destroy()
    global guessi, b2
    Label(t, text=f"Attempts Remaining: {attempts}").grid(row=0, column=0)
    guessi = Entry(t, width=20, borderwidth=20)
    guessi.insert(0, "Enter Your Guess: ")
    guessi.bind("<FocusIn>", onclick)
    guessi.grid(row=1, column=0)
    b2 = Button(t, text="Try", command=Try)
    b2.grid(row=2, column=0)
b1 = Button(t, text="Start", command=start)
b1.grid(row=2, column=0)
t.mainloop()

第50行: 我的问题是,当玩家尝试变为零时,一些标签应该被销毁,就像我在If语句中写的那样,但是..它们没有被销毁。 我的心都快碎了,我不知道为什么会这样,请帮帮我