我真的陷入了一个问题。 这是我的代码,我正在尝试做一个猜字游戏:
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语句中时销毁,但是..它们并没有销毁。 我的想法在吹,我不知道为什么会发生这种情况,请帮忙!