Python和QT返回"无"解析PDML文件时

时间:2014-06-13 09:42:33

标签: python parsing pyqt4

我正在尝试使用WiresharkXML python模块解析PDML文件。我想从计算PDML文件中的数据包数量开始。

当我创建一个小命令行程序时它工作正常,当我尝试在QT GUI中产生相同的结果时,它会一直返回答案为" none",它应该是一个整数。

它好像调用def packet_cb(数据包)有问题:代码部分可能?

无论如何,这是我的代码

import WiresharkXML
import sys
from PyQt4 import QtCore, QtGui


num_packets = 0

    def showDialog(self):
        filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
        filename2 = str (filename)

        pdml = open(filename2)
        num_packets = WiresharkXML.parse_fh(pdml, packet_cb)
        self.lineEdit.setText('The PDML File Contains '+ str(num_packets)+' Packets')

def packet_cb(packet):
    "Called once for each packet."
    global num_packets
    num_packets += 1


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    PDMLPacketCounter = QtGui.QWidget()
    ui = Ui_PDMLPacketCounter()
    ui.setupUi(PDMLPacketCounter)
    PDMLPacketCounter.show()
    sys.exit(app.exec_())

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

show_dialog中,您将分配一个局部变量num_packets,该变量以相同的名称隐藏全局变量。我怀疑局部变量是None,并且全局变量具有您期望的整数。如果您WireSharkXML.parse_fh的返回值无用,则只需在没有作业的情况下调用它:

def showDialog(self):
    filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
    filename2 = str (filename)

    pdml = open(filename2)
    WiresharkXML.parse_fh(pdml, packet_cb)   # NOTE: No assignment on this line any more!
    self.lineEdit.setText('The PDML File Contains '+ str(num_packets)+' Packets')