为什么我在Python Tkinter中得到这个TclError

时间:2015-05-27 11:15:11

标签: python tkinter tk

我的tkinter / python工作遇到了麻烦,我正在为一个学校项目创作一个混乱的游戏。我一直收到错误信息,我不知道它意味着什么。

这是它自己的代码:

fo = open("clues.txt", 'r')
clues = fo.read()
clueList = clues.split(",")

tries = 3

def check_answer(tries, answer, origword, clues):
    global playing
    if answer==origword.strip():
        tk.Label (root, text=("Correct!"), font=("Buxton Sketch", 16)).pack()
        tk.Label (root, text="Do you want to play again?", font=("Buxton Sketch", 16)).pack()
    tk.Button (root, text="Yes", font=("Buxton Sketch", 20), command=lambda: ABC("A")).pack()
    tk.Button (root, text="No", font=("Buxton Sketch", 20), command=lambda: main_menu(ABC)).pack()

def get_guess():
    global answer
    answer = tk.Entry(root, width=40, font=("Buxton Sketch"))

这是错误:

Exception in Tkinter callback
Traceback (most recent call last):
    File "C:\Python34\Assessment Word Jumble\lib\tkinter\__init__.py", line 1533, in __call__
        return self.func(*args)
    File "C:\Python34\Assessment Word Jumble\attempt@wordjumbletkinter.py", line74, in <lambda>
        w = tk.Button(root, text="A-Play Word Jumble", command=lambda: ABC("A")).pack()#(row=3, column=1)
    File "C:\Python34\Assessment Word Jumble\attempt@wordjumbletkinter.py", line 22, in ABC
        get_guess()
    File "C:\Python34\Assessment Word Jumble\attempt@wordjumbletkinter.py", line 63, in get_guess
        answer = tk.Entry (root, width=40, font=("Buxton Sketch"))
    File "C:\Python34\Assessment Word Jumble\lib\tkinter\__init__.py", line 2514, in __init__
        Widget.__init__(self, master, 'entry', cnf, kw)
    File "C:\Python34\Assessment Word Jumble\lib\tkinter\__init__.py", line 2122, in __init__
        (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: expected integer but got "Sketch"

正如我所说,我不知道这意味着什么,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

font窗口小部件的Entry参数必须是tuple('font_name', size)size是可选的)。试试这个:

def get_guess():
    global answer

    # ** Change this **
    # answer = tk.Entry(root, width=40, font= ("Buxton Sketch"))

    # ** To this **
    answer = tk.Entry(root, width=40, font=("Buxton Sketch", 16))

    # ** this will work too **
    answer = tk.Entry(root, width=40, font=("Buxton Sketch",))