我收到错误“TypeError:'NoneType'对象不可调用” 所以我为我的GUI编写了一些代码,在选择文件后我想要一个新页面。
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
答案 0 :(得分: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
(提供()
),但您也不接受参数(因此删除了额外的{{1}}在上面的代码中也是错误的。)
我不确定你想做什么。