2014-07-09 52 views
0

我得到了一個「TypeError:'NoneType'對象不可調用的錯誤」 所以我已經爲我的GUI編寫了一些代碼,並且在選擇文件後我想要一個新頁面。頁面沒有顯示爲GUI python

class Application(tk.Frame):    
    def __init__(self, master=None): 
     tk.Frame.__init__(self, master) 
     self.grid()      
     self.quit_program() 
     self.browse_file()(self, text="Create new window", 
           command=self.create_window) 
     self.button.pack(side='top') 

    def create_window(self): 
     self.counter += 1 
     t = tk.Toplevel(self) 
     t.wm_title("Window #%s" % self.counter) 
     l = tk.Label(t, text="This is window #%s" % self.counter) 
     l.pack(side="top", fill="both", expand=True, padx=100, pady=100) 

     self.file_opt = options = {} 
     options['defaultextension'] = '.txt' 
     options['filetypes'] = [('musicfiles', '.mp3'),('videofiles','.mp4')] 

     options['parent'] = self 
     options['title'] = 'This is a title' 

    def quit_program(self): 
     self.quitButton = tk.Button(self, text='Quit', 
      command=self.quit)    
     self.quitButton.grid() 

    def browse_file(self): 
     self.browseButton = tk.Button(self, text='Browse',command=self.askopenfile) 
     self.browseButton.grid() 
    def askopenfile(self): 
     return tkFileDialog.askopenfile(**self.file_opt) 

app = Application()      
app.master.title('Sample application')  
app.mainloop() 

回溯是:

Traceback (most recent call last): 
File "C:/Users/121794/Desktop/FYPtestGUI.py", 
line 39, in <module> app = Application() 
File "C:/Users/121794/Desktop/FYPtestGUI.py", 
line 11, in init command=self.create_window) 
TypeError: 'NoneType' object is not callable 
+1

請*總是*發佈完整的回溯。特別是在這種情況下,它會將您指向代碼中的失敗行。 – msvalkon

+0

@msvalkon對不起,只是該網站不允許我上傳整個代碼 – user3817491

+0

@msvalkon Traceback(最近呼叫最後): 文件「C:/Users/121794/Desktop/FYPtestGUI.py」,第39行,在 應用程式=應用程序() 文件 「C:/Users/121794/Desktop/FYPtestGUI.py」,第11行,在__init__ 命令= self.create_window) 類型錯誤: 'NoneType' 對象不是可調用 – user3817491

回答

1

這裏:

self.browse_file()(self, 
    text = "Create new window", 
    command = self.create_window 
) 

要調用browse_file(),期待它返回一個函數,調用該函數。現在這個:

TypeError: 'NoneType' object is not callable 

這是告訴你,browse_file()返回None,你的說法。 None(類型爲NoneType的對象)不是函數,也就是is not callable

您的browse_file代碼:

def browse_file(self): 
    self.browseButton = tk.Button(self, text='Browse',command=self.askopenfile) 
    self.browseButton.grid() 

你不返回任何明確的,所以這個函數返回None(給None is not callable),但不接受任何參數(因此除去在多餘的()上面的代碼也是錯誤的)。

我不確定你想要做什麼。

+0

我期待選擇file.quite後去新的一頁,但困惑於如何做到這一點。 – user3817491

+0

我很困惑:)如果你不能向我們解釋,你應該先嚐試並更好地理解問題。當我發現自己陷入困境時,我所做的一件事是花一個小時,也許是兩個小時來閱讀文檔,博客文章和示例。理解使得一切都變得簡單了,即使你沒有閱讀如何解決你當前的具體問題。 – slezica