打开文件对话框,,from tkinter


from tkinter import *from tkinter.filedialog import askopenfilenamefrom tkinter.messagebox import showerrorclass 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"),                                           ("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)            returnif __name__ == "__main__":    MyFrame().mainloop()#该片段来自于http://byrx.net

评论关闭