【发布时间】:2018-01-03 12:11:42
【问题描述】:
我对这篇文章中的代码有疑问 filedialog, tkinter and opening files
我想在我自己的代码中实现它,但是当我运行它时(没有我的代码,只有你看到的代码)显示的所有文件夹都是空的,我实际上无法打开任何东西。
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showerror
class MyFrame(Frame):
def __init__(self):
Frame.__init__(self)
self.master.title("Example")
self.master.rowconfigure(5, weight=1)
self.master.columnconfigure(5, weight=1)
self.grid(sticky=W+E+N+S)
self.button = Button(self, text="Browse", command=self.load_file, width=10)
self.button.grid(row=1, column=0, sticky=W)
def load_file(self):
fname = askopenfilename(filetypes=(("Template files", "*.tplate"),
("HTML files", "*.html;*.htm"),
("Python file", "*.py"),
("All files", "*.*") ))
if fname:
try:
print("""here it comes: self.settings["template"].set(fname)""")
except: # <- naked except is a bad idea
showerror("Open Source File", "Failed to read file\n'%s'" % fname)
return
if __name__ == "__main__":
MyFrame().mainloop()
【问题讨论】:
-
您是否在打开的对话框中选择了
All files?将其放在filetypes列表的开头,使其成为默认值。 -
谢谢!将
All Files放在开头可以解决问题。 @zwer
标签: python user-interface tkinter filedialog