2013-05-31 17 views
-1

我有一個包含8個複選框的UI。這個想法是,根據哪些被檢查,它將選擇發送到telnet的命令以及返回哪些數據文件。使用python中的複選框來選擇要寫入/導出的文件

目前我只有8個IF語句。這會導致一些文件在寫入時混在一起。我認爲能夠解決這個問題的是一個更長的陳述,其中包含所有可能的組合,但這是很多組合。有沒有簡單的方法,以便這些陳述不會相互覆蓋?

下面是一些代碼:

 if self.EHbox.isChecked(): 
      tn.write("geh,".encode('ascii') + TS2Dur.encode() + b"\n\r") 
      out_file = open(self.linePATH.text() + date + "_001.csv", "wt") 
      out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode()) 
      out_file.close()    

     if self.AHbox.isChecked(): 
      tn.write("DAT,".encode('ascii') + TS2Dur.encode() + b"\n\r") 
      out_file = open(self.linePATH.text() + date + "_001.csv", "wt") 
      out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode()) 
      out_file.close() 

     if self.DHbox.isChecked(): 
      tn.write("GDH,".encode('ascii') + TS2Dur.encode() + b"\n\r") 
      out_file = open(self.linePATH.text() + date + "_001.csv", "wt") 
      out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode()) 
      out_file.close()    

     if self.L1box.isChecked(): 
      tn.write("gl1,".encode('ascii') + TS2Dur.encode() + b"\n\r") 
      out_file = open(self.linePATH.text() + date + "_001.csv", "wt") 
      out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode()) 
      out_file.close()   

     if self.L2box.isChecked(): 
      tn.write("gl2,".encode('ascii') + TS2Dur.encode() + b"\n\r") 
      out_file = open(self.linePATH.text() + date + "_001.csv", "wt") 
      out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode()) 
      out_file.close()    

     if self.CMbox.isChecked(): 
      tn.write("gsf,0".encode('ascii') + b"\n\r") 
      out_file = open(self.linePATH.text(), "wt") 
      out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode()) 
      out_file.close()    

     if self.CNbox.isChecked(): 
      tn.write("gsf,1".encode('ascii') + b"\n\r") 
      out_file = open(self.linePATH.text(), "wt") 
      out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode()) 
      out_file.close()    

     if self.FLbox.isChecked(): 
      tn.write("gsf,2".encode('ascii') + b"\n\r") 
      out_file = open(self.linePATH.text(), "wt") 
      out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode()) 
      out_file.close() 
+0

你應該嘗試重構你的區塊 - 它們幾乎完全相同...... – Stephen

+0

@Stephen我不確定'重構你的區塊'是什麼意思。你能否詳細說明那是什麼以及如何做到這一點。也將解決我的問題? – mad5245

回答

1

這是我會怎麼做。

首先我會定義一個返回哪個開關。我會做一個4開關的例子,因爲它與8鍵相同,除了少打字。

創建每個按鈕狀態的列表(例如:[True,False,False,True] =第一,第四開關命中)

def checkSwitches(): 
    return [EHbox.isChecked(),AHbox.isChecked(),DHbox.isChecked(),L1box.isChecked()] 

然後某種For循環,無論你需要「做」的東西..也許你點擊後「轉到」按鈕

def clickGo(): 
    buttons = ['geh,','DAT,','GDH,','gl1,'] 
    for item in xrange[4]: #<4 is total number of switches 
     if checkSwitches()[item]: #checking for Trues (which im assuming 'isChecked()' returns) 
      doButtonJunk(buttons[item]) 

也是你的事情或許可以被改寫,因爲你有8幾乎同樣的事情,這樣做

def doButtonJunk(THEDIFFERENCEBETWEENYOUR8THINGS) 
     tn.write(THEDIFFERENCEBETWEENYOUR8THINGS.encode('ascii') + b"\n\r") 
     out_file = open(self.linePATH.text(), "wt") 
     out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode()) 
     out_file.close() 
從這裏

可以修改doButtonJunk()與律條if語句,如果它需要的`+日期+「_ 001.cvs」的一部分或不...可能做這樣的事情

def doButtonJunk(THEDIFFERENCEBETWEENYOUR8THINGS) 
     wt = 'wt' # made new placeholder for it 
     if THEDIFFERENCEBETWEENYOUR8THINGS in ['gl2,','GDH,'......]: #i diddnt finish this list, because im lazy.. either way its gonna check to see if it needs the date+ part, and then modify the 'wt' to be what it suppsoed to be. 
      wt = date+'_001.csv'+wt 
     tn.write(THEDIFFERENCEBETWEENYOUR8THINGS.encode('ascii') + b"\n\r") 
     out_file = open(self.linePATH.text(), wt) #inserted wt instead of 'wt' because we defined it now 
     out_file.write(tn.read_until(b"EOF").replace(b'\r\n',b'\n').replace(b'ACK',b'').replace(b'EOF',b'').strip().decode()) 
     out_file.close() 
+0

這是有道理的。還有很多事情要做,我想我認爲有一個簡單的方法來處理這個... – mad5245

相關問題