-4

In a onClick_button event I've a if condition to show a messagedialog if condition fails or else execute the rest of the statements. Basically if condition is checking whether a textctrl has value or not. If there is value execute the else statement.

this works for the first time with out any value in tc(textctrls) with a msgdlg, but when click ok on the dlg and add some value in the tc, still the msgdlg pops out when it should execute the else.

Your help is very appreciated. I've checked all the indentation.

def onClick_button_new(self, event):

    self.list_box.Clear()
    varstr = "csv"


    if [(self.tc1.GetValue() == "") or (self.tc2.GetValue() == "")]:
        dial = wx.MessageDialog(None, 'No file specified.  Please specify relevant file', 'Error', wx.OK)
        dial.ShowModal()
    else:
        file1 = subprocess.check_output("cut -d '.' -f2 <<< %s" %self.var1, shell = True, executable="bash")
        file1type = file1.strip()
        print file1type
        file2 = subprocess.check_output("cut -d '.' -f2 <<< %s" %self.var2, shell = True, executable="bash")
        file2type = file2.strip()
        if varstr in (file1type, file2type):
            print "yes"
        else:
            dial = wx.MessageDialog(None, ' Invalid file format.  Please specify relevant file', 'Error', wx.OK)
            dial.ShowModal()
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61

3 Answers3

1

depending on your input

[(self.tc1.GetValue() == "") or (self.tc2.GetValue() == "")]

is either [True] or [False]. in any case a non-empty list. this will be interpreted as True;

if [False]:
    do_something()

in this example do_something() will always be executed.

to fix this you need to remove the brackets []:

if (self.tc1.GetValue() == "") or (self.tc2.GetValue() == ""):
    ...
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
1

You have your boolean logic mixed up and created a list object (which is always non-empty and always true). You want to use and and not use a list:

if self.tc1.GetValue() == "" and self.tc2.GetValue() == "":

You should never use a [...] list for a boolean test, because that just creates a list object that is not empty, and thus always considered true in a boolean context, regardless of the contained comparison results:

>>> [0 == 1 or 0 == 1]
[False]
>>> bool([0 == 1 or 0 == 1])
True
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

Don't need to compare your method result to empty string :

if not self.tc1.GetValue() and not self.tc2.GetValue():

See that post : Most elegant way to check if the string is empty in Python?

Community
  • 1
  • 1
Wilfried
  • 1,623
  • 1
  • 12
  • 19