
Python Tkinter Open File Dialog


Python Open Folder Dialog
Python 3.4'Open a file dialog window in tkinter using the filedialog method.Tkinter has a prebuilt dialog window to access files.This example is designed to show how you might use a file dialog askopenfilenameand use it in a program.' 'from tkinter import.from tkinter import ttkfrom tkinter.filedialog import askopenfilenameroot = Tk( )#This is where we lauch the file manager bar.def OpenFile:name = askopenfilename( initialdir = 'C:/Users/Batman/Documents/Programming/tkinter/ ',filetypes =(( 'Text File ', '.txt '),( 'All Files ', '. ')),title = 'Choose a file. ')print (name)#Using try in case user types in unknown file or closes without choosing a file.try:with open(name, 'r ') as UseFile:print(UseFile.read)except:print( 'No file exists ')Title = root.title( 'File Opener ')label = ttk.Label(root, text = 'I'm BATMAN!!! ', foreground = 'red ', font =( 'Helvetica ', 16))label.pack#Menu Barmenu = Menu(root)root.config( menu =menu)file = Menu(menu)file.addcommand( label = 'Open ', command = OpenFile)file.addcommand( label = 'Exit ', command = lambda: exit)menu.addcascade( label = 'File ', menu = file)root.mainloop'RESULTS:C:/Users/Scott/Documents/Programming/json/test.txt'I like to move it, groove it!'