-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathclass_filedialog.py
More file actions
37 lines (27 loc) · 883 Bytes
/
class_filedialog.py
File metadata and controls
37 lines (27 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from tkinter import *
from tkinter import filedialog
class App(Tk):
def __init__(self):
super().__init__()
# Title, icon, size
self.title("Tkinter.com - OOP File Dialog")
self.iconbitmap('images/codemy.ico')
self.geometry('700x450')
# Create Widgets
self.my_text = Text(self, width=80, height=20)
self.my_text.pack(pady=20)
self.my_button = Button(self, text="Open File", command=self.file)
self.my_button.pack(pady=20)
# Create Popup Function
def file(self):
self.my_file = filedialog.askopenfilename(initialdir="",
title="Select a File",
filetypes=(("txt files", "*.txt"), ("All Files", "*.*")))
# Check to make sure user selected a file
if self.my_file:
# Open and read the file
get_contents = open(self.my_file, "r")
self.my_text.insert(END, get_contents.read())
# Define and instantiate our app
app = App()
app.mainloop()