Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

askopenfilename handling cancel on dialogue

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))
like image 336
BAI Avatar asked Feb 21 '13 19:02

BAI


1 Answers

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

like image 169
canyonblue77 Avatar answered Oct 16 '22 12:10

canyonblue77