python if条件未按预期正常工作

python if条件未按预期正常工作,python,Python,在onClick_按钮事件中,如果条件失败,我有一个if条件来显示messagedialog,否则执行其余语句。 基本上,如果条件是检查textctrl是否有值。如果有值,则执行else语句 这是msgdlg首次在tc(textctrls)中没有任何值的情况下工作,但是当单击dlg上的ok并在tc中添加一些值时,msgdlg仍然会在应该执行else时弹出 非常感谢你的帮助。 我检查了所有的压痕 def onClick_button_new(self, event): self.list

在onClick_按钮事件中,如果条件失败,我有一个if条件来显示messagedialog,否则执行其余语句。 基本上,如果条件是检查textctrl是否有值。如果有值,则执行else语句

这是msgdlg首次在tc(textctrls)中没有任何值的情况下工作,但是当单击dlg上的ok并在tc中添加一些值时,msgdlg仍然会在应该执行else时弹出

非常感谢你的帮助。 我检查了所有的压痕

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()
def onClick_按钮_new(self,event):
self.list_box.Clear()
varstr=“csv”
如果[(self.tc1.GetValue()=”)或(self.tc2.GetValue()=”)]:
dial=wx.MessageDialog(无,'未指定文件。请指定相关文件','错误',wx.OK)
拨号:ShowModal()
其他:

file1=子进程。根据您的输入检查输出(“cut-d”。-f2

[(self.tc1.GetValue() == "") or (self.tc2.GetValue() == "")]
[True]
[False]
。在任何情况下都是非空列表。这将被解释为
True

if [False]:
    do_something()
在本例中,将始终执行
do\u something()

要解决此问题,您需要删除括号
[]

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

您将布尔逻辑混合在一起,并创建了一个列表对象(始终为非空且始终为true)。您希望使用
以及而不是使用列表:

if self.tc1.GetValue() == "" and self.tc2.GetValue() == "":
对于布尔测试,绝不应使用
[…]
列表,因为这只会创建一个非空的列表对象,因此在布尔上下文中始终被视为true,而不管包含的比较结果如何:


不需要将方法结果与空字符串进行比较:

if not self.tc1.GetValue() and not self.tc2.GetValue():
见该帖子:

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