QLineEdit 和自定义函数不兼容



我正在尝试用Python和PyQt4完成我的项目,但是通过我创建的函数传递QLineEdit变量时遇到了问题。该字符串应该用作 url,当我通过我的第一个参数传递它时,它试图读取 url 并获取其内容,它会给我抛出此错误:

Traceback (most recent call last):
  File "programa2.py", line 158, in on_link_clicked
    download_mango(self.a, self.path2)
  File "c:UsersPobletManGetmango.py", line 19, in download_mango
    urlContent = urllib2.urlopen(url).read() # We read the URL
  File "c:Python27liburllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "c:Python27liburllib2.py", line 386, in open
    protocol = req.get_type()
AttributeError: 'QString' object has no attribute 'get_type'

由以下操作触发:

def on_link_clicked(self):
    self.a = self.linkEdit.displayText()
    download_mango(self.a, self.path2)

我完全迷失了。可能是 PyQt4 问题或我的函数出了问题?

我感谢您的帮助。

你没有发布足够的代码来证明你的陈述

字符串应该作为一个 url 工作,当我通过我的第一个参数传递它时

看起来您正在将QString传递到urlopen中。 只需将其包裹在str()中,您应该没问题。

>>> url = QString('http://stackoverflow.com/questions/11121475')
>>> urllib2.urlopen(url).read()
### this generates your error ending with
AttributeError: 'QString' object has no attribute 'get_type'
>>> urllib2.urlopen(str(url)).read()
### works

self.a 缺失

"http://"或"https://"

尝试

download_mango("http://"+self.a,self.path2)

见 http://www.nullege.com/codes/search/urllib2.Request.get_type

最新更新