Advertisement
steve-shambles-2109

Python code snippets vol 33: 163-File browser and executor

Oct 13th, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. """
  2. Python code snippets vol 33:
  3. 163-File browser and executor
  4. stevepython.wordpress.com
  5.  
  6. Source:
  7. Steve Shambles Oct 2019
  8. Knocked this up to save time when I need a file selector
  9. in my Tkinter GUI.
  10.  
  11. Added the execute file bit to make it a bit more interesting.
  12. """
  13. from tkinter import filedialog, Tk, Button, Entry, LabelFrame
  14. import webbrowser
  15.  
  16. root = Tk()
  17. root.title('File selector')
  18.  
  19. def get_file():
  20.     """Get file from user and execute its linked app"""
  21.     users_file = filedialog.askopenfilename(title='Please Select a file')
  22.     entry1.delete(0, 'end')
  23.     entry1.insert(0, users_file)
  24.     webbrowser.open(users_file)
  25.  
  26. frame1 = LabelFrame(root, text='Click browse to choose a file')
  27. frame1.grid(padx=20, pady=10)
  28.  
  29. entry1 = Entry(frame1)
  30. entry1.grid(padx=5, pady=5)
  31.  
  32. btn1 = Button(frame1, text='Browse', command=get_file)
  33. btn1.grid(row=0, column=1, padx=8, pady=15)
  34.  
  35. root.mainloop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement