2012-04-02 97 views
0

我從Tkinter和Python的GUI開始。 我正在嘗試爲遊戲創建圖形界面。 當我試圖運行這個程序:python 2.7:Tkinter錯誤:#TclError可能會在這裏引發

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

回答

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的更多信息。

+0

其實,我學到了一些關於MAC OS X Python安裝程序和Tcl/Tk版本的信息。我終於安裝了適合我的操作系統的Python: 謝謝! – 2012-04-04 11:59:43