2016-12-30 75 views
-4

在onClick_button事件我有一個if條件來顯示messagedialog如果條件失敗或執行其餘的語句。 基本上,如果條件是檢查textctrl是否有價值或不。如果有值執行else語句。蟒蛇如果條件不能正常工作如預期

這是第一次在tc(textctrls)中帶有msgdlg的任何值,但是當在dlg上單擊確定並在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() 

回答

1

根據輸入

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

或者是[True][False]。在任何情況下都是非空的列表。這將被解釋爲True;

if [False]: 
    do_something() 
在這個例子中 do_something()

就一定會執行。

解決這個問題,你需要刪除括號[]

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

非常感謝。這工作。 –

1

你有你的布爾邏輯混合起來,創建一個列表對象(它總是非空和總是真)。你想用and使用列表:

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

你不應該使用[...]列表一個布爾測試,因爲這只是創建一個列表對象不是空的,因而總是被視爲真正的布爾上下文,不管所包含的比較結果

>>> [0 == 1 or 0 == 1] 
[False] 
>>> bool([0 == 1 or 0 == 1]) 
True 
+0

非常感謝。那工作。 –