python 2.7:Tkinter错误:这里可能会引发#TclError

时间:2012-04-02 19:28:07

标签: python tkinter

我从使用Tkinter开始使用Python进行GUI。 我正在尝试为游戏创建图形界面。 当我试图运行这个程序时:

    import Tkinter as tk
    import ttk

    def create_frame(parent, col, r, st):
        frame = ttk.Frame(parent, padding="3 3 12 12")
        frame.grid(column=col, row=r, sticky=st)
        frame.columnconfigure(0, weight=1)
        frame.rowconfigure(0, weight=1)
        return frame
    def create_frame(parent, col, r, st):
        frame = ttk.Frame(parent, padding="3 3 12 12")
        frame.grid(column=col, row=r, sticky=st)
        frame.columnconfigure(0, weight=1)
        frame.rowconfigure(0, weight=1)       
        return frame
    def create_entry(parent, col, r, st, w):
        val = tk.StringVar()
        entry = ttk.Entry(parent, width=w, textvariable=val)
        entry.grid(column=col, row=r, sticky=st)
        return val, entry
    def create_label(parent, col, r, st, text = None):
        if text == None:
            val = tk.StringVar()
            label = ttk.Label(parent, textvariable = val)
        else:
            label = ttk.Label(parent, text = text)
            val = None
        label.grid(column = col, row = r, sticky = st)
        return val
    def create_button(parent, col, r, st, com, text):
        button = ttk.Button(parent, text = text, command = com)
        button.grid(column = col, row = r, sticky = st)
    def create_main_window():
        root = tk.Tk()
        root.title("Scrabble")
        main_frame = create_frame(root, col = 0, r = 0, st = (tk.N, tk.W,     tk.E, tk.S))
        feet_val, feet_entry = create_entry(main_frame, col = 2, r = 1, st =     (tk.W, tk.E), w = 7)
        meters = create_label(main_frame, col = 2, r = 2, st = (tk.W, tk.E))
        create_label(main_frame, text = "feet", col = 3, r = 1, st = tk.W)
        create_label(main_frame, text = "is equivalent to", col = 1, r = 2,     st = tk.E)
        create_label(main_frame, text = "meters", col = 3, r = 2, st = tk.W)
        for child in main_frame.winfo_children():
            child.grid_configure(padx=5, pady=5)

        feet_entry.focus()
        root.mainloop()

    create_main_window()

但我收到此错误消息:

  File "Scrabble.py", line 175, in <module>
    create_main_window()
  File "Scrabble.py", line 160, in create_main_window
    main_frame = create_frame(root, col = 0, r = 0, st = (tk.N, tk.W, tk.E,     tk.S))
  File "Scrabble.py", line 131, in create_frame
    frame = ttk.Frame(parent, padding="3 3 12 12")
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-    tk/ttk.py", line 761, in __init__
    Widget.__init__(self, master, "ttk::frame", kw)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-    tk/ttk.py", line 559, in __init__
    _load_tile(master)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-    tk/ttk.py", line 47, in _load_tile
    master.tk.eval('package require tile') # TclError may be raised here
_tkinter.TclError: can't find package tile

我该怎么办? 你有什么建议可以作为Tkinter的开头吗? 先感谢您。 MFF

1 个答案:

答案 0 :(得分:4)

根据显示的路径,您似乎在OS X上使用python.org Python 2.7。根据您运行的OS X版本以及您使用的Python安装程序,Tcl / Tk版本可能不包括ttk或瓦片,它的前身。如果您使用的是64/32位Python 2.7(版本行会说GCC 4.2.1),请尝试从here安装ActiveState Tcl / Tk 8.5。如果使用python.org 32位仅安装程序(GCC 4.0.1),请安装ActiveState Tcl / Tk 8.4。有关Mac OS X Python安装程序和Tcl / Tk版本here的更多信息。