我做了一个简单的tkinter GUI,我想要一个浏览按钮,但它似乎没有出现
#!/usr/bin/env python
import Tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.quit_program()
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=tkFileDialog.askopenfilename(parent=root,title='Open file to encrypt'))
self.browseButton.grid()
app = Application()
app.master.title('Sample application')
app.mainloop()
答案 0 :(得分:0)
#!/usr/bin/env python
import Tkinter as tk
import tkFileDialog
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.quit_program()
self.browse_file()
self.file_opt = options = {}
options['defaultextension'] = '.txt'
options['filetypes'] = [('musicfiles', '.mp3'),('vediofiles', '.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()
答案 1 :(得分:0)
代码中的三个错误
tkFileDialog
?你有进口吗?通过在文件顶部添加import tkFileDialog
来导入它root
?将root
更改为self
。command
中的tk.Button
参数并不是按照您的方式使用。您不应该在命令参数中调用该方法。您应该指定单击按钮时要调用的方法。在您的示例中,您告诉tkinter,调用tkFileDialog.askopenfilename()
返回的值而不是调用tkFileDialog.askopenfilename
。所以用另一种方法替换它或使用lambda。
command=lambda : tkFileDialog.askopenfilename(parent=root,title='Open file to encrypt')