# ========================================================= # Tk file dialog # ========================================================= # # Notes on import: # # import x # imports the module x, and creates a referenceto that # module. You need to use a complete p[ath to access a # particular attribute or method in x. # (e.g. x.name or x.attribute) # # from x import * # imports the module x and creates references to all public # objects defined by that module in the current namespace. # (That is, anything that have a name starting with _) or # whatever name you mentioned. # # All names in the modules are available in the local # namespace. # # (x itself is not defined. e.g. x.names does not work.) # # ========================================================= # --------------------------------------------------------- # --- import # --------------------------------------------------------- import sys if sys.version_info.major == 3: from tkinter import * from tkinter import filedialog as FD else: from Tkinter import * import Tkinter, Tkconstants, tkFileDialog as FD # --------------------------------------------------------- # --- global variables # --------------------------------------------------------- buttonFont = ('arial', '20', 'bold') # --------------------------------------------------------- # functions # --------------------------------------------------------- def popupOpenFileDialog(): f = FD.askopenfilename() print('Open returned type: {}'.format(type(f))) print('Open returned: {}'.format(f)) if f: print('File: {}'.format(f)) else: print('CANCEL') def popupSaveFileDialog(): f = FD.asksaveasfilename() print('Save returned type: {}'.format(type(f))) print('Save returned: {}'.format(f)) if f: print('File: {}'.format(f)) else: print('CANCEL') def quit(): sys.exit() # --------------------------------------------------------- # main # --------------------------------------------------------- if __name__ == '__main__': # -- Tk root window root = Tk() root.title('Tk Message Box') # --- buttons fb = Frame(root) fb.grid(row=0,column=0) b1 = Button(fb, text='Popup Open File Dialog', font=buttonFont, command=popupOpenFileDialog) b2 = Button(fb, text='Popup Save File Dialog', font=buttonFont, command=popupSaveFileDialog) bq = Button(fb, text='Quit', font=buttonFont, command=quit) b1.grid(row=0, column=0) b2.grid(row=0, column=1) bq.grid(row=1, column=0, columnspan=2) fb.grid(row=0, column=0) root.mainloop()