获取奇怪的_tkinter.TclError:未知选项

时间:2018-10-13 20:59:24

标签: python python-3.x tkinter

我制作了一款小型游戏,您可以像instlife一样度过自己的一生,但是可以用Tkinter用python制作。它不完整,几乎没有完成的地方,因为我总是会遇到同样的错误。

这是我的代码

import tkinter

window = tkinter.Tk()
window.geometry("275x400")
window.title("Life")

year = 1980
age = 0

def ageButton():
      global year
      global age
      year += 1
      age += 1
      yearText.configure(text=year)
      dynamicText.configure("You are %d" %age)


yearText = tkinter.Label(window, text=year, anchor="n", width="270")
dynamicText = tkinter.Label(window, text="You are %d" %age, anchor="n", 
width="270", height="495")
ageButton = tkinter.Button(window, text="Age", width="270",
command=ageButton)



ageButton.pack()
yearText.pack()
dynamicText.pack()
window.mainloop()

这是我得到的错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Ed\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "C:\Users\Ed\Desktop\Lifee2\Life2.py", line 16, in ageButton
    dynamicText.configure("You are %d" %age)
  File "C:\Users\Ed\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1482, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Users\Ed\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1472, in _configure
    return self._getconfigure1(_flatten((self._w, cmd, '-'+cnf)))
  File "C:\Users\Ed\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1460, in _getconfigure1
    x = self.tk.splitlist(self.tk.call(*args))
_tkinter.TclError: unknown option "-You are 1"

1 个答案:

答案 0 :(得分:0)

调用configure方法时,必须告诉tkinter您要更改的值。在您的情况下,您需要更改以下内容:

dynamicText.configure("You are %d" %age)

...对此:

dynamicText.configure(text="You are %d" %age)