【问题标题】:KeyError when using QString使用 QString 时出现 KeyError
【发布时间】:2017-11-23 01:19:18
【问题描述】:

我想执行哈希算法。所以,我做了这个代码。我还导入了 hashlib。但是,在最后一行,发生了错误:

KeyError: PyQt4.QtCore.QString(u'sha1')

我不知道问题是什么。代码如下:

def setupUi(self, MainWindow)
    self.radioLabel = 'sha256'  #default 256
    self.combo.currentIndexChanged.connect(self.OnRadiogroup)

def OnRadiogroup(self,radioLabel)
    self.radioLabel = radioLabel
    self.radioLabel = self.combo.currentText() #get selected hash string

def create_matchkey(self, row, path, key, radioLabel):

    hash_function = {"sha1":hashlib.sha1,
                     "sha224":hashlib.sha224,
                     "sha256":hashlib.sha256,
                     "sha512":hashlib.sha512,
                     "md5":hashlib.md5}
    hash_object = hash_function[radioLabel](self.input_str)

【问题讨论】:

  • 你必须提供minimal reproducible example,也就是说,我可以复制你的代码并运行它没有问题,但你的例子不是。

标签: python python-2.7 pyqt4 qstring keyerror


【解决方案1】:

你需要将QString从radioLabel转换成python字符串:

hash_object = hash_function[str(radioLabel)](self.input_str)

【讨论】:

    猜你喜欢
    • 2021-02-18
    • 2013-05-05
    • 1970-01-01
    • 2018-05-02
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    相关资源
    最近更新 更多