I'm trying to create a notepad-type program in Tkinter and Can't work out how to display a file's contents to a Label...
I have been able to display it's contents to the pyCharm shell but when I try to show in a label I get an error.
def openFile():
fToOpen = filedialog.askopenfilename(filetypes=[("Text files","*.txt")])
#print(fToOpen.read()) <-- ##This works##
fileToOpen = open(fToOpen, 'r')
Label(root, fileToOpen.read()).pack() <-- ##This doesn't##
fToOpen.close()
The error I get is:
Traceback (most recent call last): File "C:\Users\Hello\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__ return self.func(*args) File "C:/Users/Hello/Documents/html/Python/Prog.py", line 143, in openFile Label(root, fileToOpen.read()).pack(fill=Y) File "C:\Users\Hello\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2760, in __init__ Widget.__init__(self, master, 'label', cnf, kw) File "C:\Users\Hello\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2289, in __init__ classes = [(k, v) for k, v in cnf.items() if isinstance(k, type)] AttributeError: 'str' object has no attribute 'items'
Can anyone help me?
print(fToOpen.read()) <-- ##This works##really works?