Q:

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))

A:

.askopenfilename() returns an empty string on cancel, not None.  So you can either compare to '' or False.  For the sake of having a code sample:
def openFileClicked(self):
    self.filename = filedialog.askopenfilename()
    if not self.filename:
        # config...delete...etc.
    # Rest of function

At least that's how I'd do it, you may have your own preferences.

A:

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

