I have a gui which initializes the askopenfilename when a button is pressed but I want to be able to account for when the user selects cancel on the askopenfilename dialogue
Here is my function to handle the clicked button yet the if statement line doesnt seem to work!
def openFileClicked(self):
self.filename=filedialog.askopenfilename()
if self.filename== None:
self.e.config(state= NORMAL)
self.e.delete(0,END)
self.e.insert(0,"...")
self.e.config(state="readonly")
self.e.config(state= NORMAL)
self.e.delete(0,END)
self.e.insert(0, self.filename)
self.e.config(state="readonly")
print ((self.filename))
I know this is a few years later but I found a quirk that is related and couldn't find any information. Hope the information is useful to anyone who comes across this answer.
Basically, as stated, clicking Cancel will return an empty string... Unless you actually select/highlight a file first and then click cancel. This seems to return an empty tuple!!!
Using python 2.6.6 (IDK, ask RedHat)
Running the following code produces the subsequent results
f_picked = tkFileDialog.askopenfilename()
test = type(f_picked)
print (test)
Results:<type 'unicode'>
# Nothing selected, Cancel clicked<type 'tuple'>
# File selected, Cancel clicked<type 'str'>
# File selected, OK clicked<type 'tuple'>
# Multiple files selected, OK clicked
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With