-1

我有一个带有 8 个复选框的 UI。这个想法是,根据检查的内容,它将选择要发送到 telnet 的命令以及要返回的数据文件。

目前我只有 8 个 IF 语句。这导致某些文件在写入时混淆。我认为解决这个问题的方法是更长的 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()
4

1 回答 1

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()使用 lil if 语句进行修改,以查看它是否需要 `+date+"_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()
于 2013-05-31T21:04:29.557 回答