Dec-07-2022, 07:55 PM
Hello
I would like to know how to avoid an error when i close the window instead of choosing a file to open.
Window that show after clicking on "open" button.
I would like to know how to avoid an error when i close the window instead of choosing a file to open.
Window that show after clicking on "open" button.
from tkinter import *
from tkinter import filedialog
def openFile():
filepath = filedialog.askopenfilename(initialdir="C:\\Test",
title="Open file okay?",
filetypes=(("text files", "*.txt"),
("all files", "*.*")))
file = open(filepath, 'r')
print(file.read())
file.close()
window = Tk()
button = Button(text="Open",command=openFile)
button.pack()
window.mainloop()Error:Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Papa Smurf\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/Papa Smurf/PycharmProjects/Pythonbasics/test/open1.py", line 9, in openFile
file = open(filepath, 'r')
FileNotFoundError: [Errno 2] No such file or directory: ''
