我正在尝试使用 Tkinter 编写一个模拟 Hangman 游戏的 GUI。到目前为止,我已经让 GUI 创建了一个标签,该标签根据用户正确猜测的字母进行更新,但终端仍然给出错误:“TypeError:‘StringVar’类型的参数不可迭代”。我已经查看了此错误的其他解决方案,但无法弄清楚如何解决该问题。
它还没有完成 - 但到目前为止的代码如下:
import randomword
# from PyDictionary import PyDictionary
from tkinter import *
root = Tk()
playerWord: str = randomword.get_random_word()
word_guess: str = ''
guesses = ''
def returnEntry(arg=None):
global word_guess
global guesses
global wordLabel
word_guess = ''
# dictionary = PyDictionary()
failed = 0
incorrect = 10
result = myEntry.get()
while incorrect > 0:
if not result.isalpha():
resultLabel.config(text="that's not a letter")
elif len(result) != 1:
resultLabel.config(text="that's not a letter")
else:
resultLabel.config(text=result)
assert isinstance(END, object)
myEntry.delete(0, END)
guesses += result
for char in playerWord:
if char in guesses:
# Print the letter they guessed
word_guess += char
elif char not in guesses:
# Print "_" for every letter they haven't guessed
word_guess += "_ "
failed += 1
if guesses[len(guesses) - 1] not in playerWord:
resultLabel.config(text="wrong")
incorrect -= 1
wordLabel = Label(root, updateLabel(word_guess))
myEntry.delete(0, END)
resultLabel = Label(root, text="")
resultLabel.pack(fill=X)
myEntry = Entry(root, width=20)
myEntry.focus()
myEntry.bind("<Return>", returnEntry)
myEntry.pack()
text = StringVar()
text.set("your word is: " + ("_ " * len(playerWord)))
wordLabel = Label(root, textvariable=text)
wordLabel.pack()
def updateLabel(word):
text.set("your word is: " + word)
return text
mainloop()
当我在 wordLabel 上运行该函数时出现问题:“wordLabel = Label(root, updateLabel(word_guess))”来重置标签。关于如何在 while 循环迭代后将标签更新为 word_guess 有什么建议吗?
函数式编程
肥皂起泡泡
相关分类