【问题标题】:Python: TypeError: QString.__getitem__(): arguments did not match any overloaded callPython:TypeError:QString.__getitem__():参数不匹配任何重载调用
【发布时间】:2015-12-03 00:53:54
【问题描述】:

我正在创建一个函数,它使用字典替换文本文件中的单词,即如果在文件中遇到第一个单词对,它将被第二个替换。此外,当用户使用 GUI 中的小部件时调用创建的函数。所以创建的函数都存在于一个 ui 类中。 (希望这是有道理的)。我正在使用python

字典看起来像这样(dictlist = {'test':'rest', 'happy':'sad', 'big':'small'})。直接在 python 编辑器中编写时,该函数可以正常工作,但是当我将其复制到翻译后的 .ui 代码时,出现以下错误。任何人都可以就为什么会发生这种情况提供建议。我是新手,所以希望我的解释是有道理的。

line = rpairs(line, dictlist)
line 1486, in rpairs
temp = temp.replace(key, dictlist[key])
TypeError: QString.__getitem__(): arguments did not match any overloaded call:
overload 1: argument 1 has unexpected type 'QString'
overload 2: argument 1 has unexpected type 'QString'

def temp(self):
    import string
    global filename
    global mytemp
    dictlist = self.lineEdit_dict.text()
    filename2 = self.lineEdit_outputfilename.text()

    f1 = open(filename, 'r')      
    f2 = open('filename2', 'w')

    def  rpairs (temp, dictlist):
        for key in dictlist:
            temp = temp.replace(key, dictlist[key])
        return temp

    for line in f1:
        line = rpairs(line, dictlist)
        f2.write(line)
    f2.close()
    return mytemp

【问题讨论】:

  • 您是否试图通过阅读self.lineEdit_dict.text()dictlist 构造为字典?...或者您的程序代码中已经有了它?

标签: python user-interface qstring


【解决方案1】:

您使用 QString 而非 dict 初始化 dictlist

dictlist = self.lineEdit_dict.text()

当您尝试以dict 访问它时:dictlist[key]

你还有很多其他问题,比如f2 = open('filename2', 'w')应该是f2 = open(filename2, 'w')

【讨论】:

  • @马可。非常感谢您的回复。我已经更正了 open 语句中的类型。
  • @Marco。关于dictlist问题/评论,因为我是新手,所以我不明白我用QString而不是dict初始化dictlist是什么意思,并尝试将其作为dict访问。您对我如何解决此问题有任何建议吗?我对 QString 的理解不是很深。
  • @bigbrother:关于 dictlist 问题/评论,因为我是新手,我不明白我用 QString 而不是 dict 初始化 dictlist 的意思,并尝试将其作为字典。您对我如何解决此问题有任何建议吗?我对 QString 的理解不是很深。
  • 抱歉,我的意思是@bigOTHER :)
  • @sren self.lineEdit_dict.text() 是文本,QString 不是 dict,您无法通过按键访问它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-12
  • 2016-12-16
  • 2018-04-02
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多