【问题标题】:Tkinter TypeError: argument of type 'StringVar' is not iterableTkinter TypeError:“StringVar”类型的参数不可迭代
【发布时间】:2020-10-08 22:59:07
【问题描述】:

我正在尝试使用 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 的任何建议?

【问题讨论】:

  • 能否包含 python 回溯,以便我们可以看到完整的错误文本和失败行?

标签: python user-interface tkinter


【解决方案1】:

此行导致错误:

wordLabel = Label(root, updateLabel(word_guess))

你想多了。应该是这样的:

updateLabel(word_guess)

您还有另一个重大错误。这一行:

while incorrect > 0:

将导致您的程序锁定。您需要将其更改为:

if incorrect > 0:

【讨论】:

    【解决方案2】:
    wordLabel = Label(root, updateLabel(word_guess))
    

    您尝试创建一个新的Label 并使全局wordLabel 引用新标签而不是旧标签。即使它工作正常,这也不会更新您的 GUI,因为新标签没有打包。

    它中断的原因是,尽管updateLabel 更改了名为textStringVar 的内容并将其返回,但它并没有被用作新标签的textvariable。除了指定父窗口小部件之外,您应该只对构造函数使用关键字参数,否则参数的解释可能与您期望的不同。 (坦率地说,我很惊讶你能以这种方式调用函数;我本来希望在调用时出现TypeError。)

    无论如何,您需要做的所有事情 - 因为 text 也是全局的 - 直接将 .set 它添加到新文本中。这将自动更新wordLabel 的外观(以任何通常刷新显示所需的 tkinter 内容为模)——这就是 StringVar 容器类的重点(而不是仅使用纯字符串)。

    【讨论】:

      猜你喜欢
      • 2021-01-15
      • 2011-10-04
      • 2019-08-24
      • 2020-05-07
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多