TypeError: 'in <string>' requires string as left operand, not QString
TypeError: 'in <string>' requires string as left operand, not QString
我正在尝试编写一个简单的应用程序来从我的 gmail 帐户发送(并可能接收)电子邮件。我在我的源代码中硬编码我的帐户信息时设法做到了,但现在我想在 GUI 字段中输入它们并从那里读取信息。这是代码:
import sys
import smtplib
from PyQt4 import QtCore, QtGui
from Notifier_Main import Ui_Notifier_Main_GUI
class MainGUI(QtGui.QWidget, Ui_Notifier_Main_GUI):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)
self.sendButton.clicked.connect(self.send)
def send(self):
fromaddr = self.senderEmailLineEdit.text()
toaddrs = self.receiverEmailLineEdit.text()
msg = self.msgTextEdit.toPlainText()
username = self.senderEmailLineEdit.text()
server = smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login(username, 'password')
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
main_gui = MainGUI()
main_gui.show()
sys.exit(app.exec_())
当我 运行 它时,我得到这个长屁股错误:
C:\Python27\python.exe "E:/Python Projekti/Notifier/src/main.py"
Traceback (most recent call last):
File "E:/Python Projekti/Notifier/src/main.py", line 20, in send
server.sendmail(fromaddr, toaddrs, msg)
File "C:\Python27\lib\smtplib.py", line 728, in sendmail
(code, resp) = self.mail(from_addr, esmtp_opts)
File "C:\Python27\lib\smtplib.py", line 480, in mail
self.putcmd("mail", "FROM:%s%s" % (quoteaddr(sender), optionlist))
File "C:\Python27\lib\smtplib.py", line 141, in quoteaddr
m = email.utils.parseaddr(addr)[1]
File "C:\Python27\lib\email\utils.py", line 214, in parseaddr
addrs = _AddressList(addr).addresslist
File "C:\Python27\lib\email\_parseaddr.py", line 457, in __init__
self.addresslist = self.getaddrlist()
File "C:\Python27\lib\email\_parseaddr.py", line 218, in getaddrlist
ad = self.getaddress()
File "C:\Python27\lib\email\_parseaddr.py", line 228, in getaddress
self.gotonext()
File "C:\Python27\lib\email\_parseaddr.py", line 204, in gotonext
if self.field[self.pos] in self.LWS + '\n\r':
TypeError: 'in <string>' requires string as left operand, not QString
我尝试用谷歌搜索该类型错误,并发现了一些关于某些 spyderlib 的 link 但由于我对这一切都很陌生,所以我不知道该怎么做。
大多数对具有文本的 Qt 元素的请求将 return QString,这是 Qt 使用的简单字符串容器。大多数其他库都需要常规 python 字符串,因此可能需要使用 str() 进行转换。全部:
fromaddr = self.senderEmailLineEdit.text()
toaddrs = self.receiverEmailLineEdit.text()
msg = self.msgTextEdit.toPlainText()
username = self.senderEmailLineEdit.text()
是 QString 对象。
尝试使用内置函数 str() 将变量转换为字符串
只需使用这个:
fromaddr = str(self.senderEmailLineEdit.text())
toaddrs = str(self.receiverEmailLineEdit.text())
msg = str(self.msgTextEdit.toPlainText())
username = str(self.senderEmailLineEdit.text())
我正在尝试编写一个简单的应用程序来从我的 gmail 帐户发送(并可能接收)电子邮件。我在我的源代码中硬编码我的帐户信息时设法做到了,但现在我想在 GUI 字段中输入它们并从那里读取信息。这是代码:
import sys
import smtplib
from PyQt4 import QtCore, QtGui
from Notifier_Main import Ui_Notifier_Main_GUI
class MainGUI(QtGui.QWidget, Ui_Notifier_Main_GUI):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)
self.sendButton.clicked.connect(self.send)
def send(self):
fromaddr = self.senderEmailLineEdit.text()
toaddrs = self.receiverEmailLineEdit.text()
msg = self.msgTextEdit.toPlainText()
username = self.senderEmailLineEdit.text()
server = smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login(username, 'password')
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
main_gui = MainGUI()
main_gui.show()
sys.exit(app.exec_())
当我 运行 它时,我得到这个长屁股错误:
C:\Python27\python.exe "E:/Python Projekti/Notifier/src/main.py"
Traceback (most recent call last):
File "E:/Python Projekti/Notifier/src/main.py", line 20, in send
server.sendmail(fromaddr, toaddrs, msg)
File "C:\Python27\lib\smtplib.py", line 728, in sendmail
(code, resp) = self.mail(from_addr, esmtp_opts)
File "C:\Python27\lib\smtplib.py", line 480, in mail
self.putcmd("mail", "FROM:%s%s" % (quoteaddr(sender), optionlist))
File "C:\Python27\lib\smtplib.py", line 141, in quoteaddr
m = email.utils.parseaddr(addr)[1]
File "C:\Python27\lib\email\utils.py", line 214, in parseaddr
addrs = _AddressList(addr).addresslist
File "C:\Python27\lib\email\_parseaddr.py", line 457, in __init__
self.addresslist = self.getaddrlist()
File "C:\Python27\lib\email\_parseaddr.py", line 218, in getaddrlist
ad = self.getaddress()
File "C:\Python27\lib\email\_parseaddr.py", line 228, in getaddress
self.gotonext()
File "C:\Python27\lib\email\_parseaddr.py", line 204, in gotonext
if self.field[self.pos] in self.LWS + '\n\r':
TypeError: 'in <string>' requires string as left operand, not QString
我尝试用谷歌搜索该类型错误,并发现了一些关于某些 spyderlib 的 link 但由于我对这一切都很陌生,所以我不知道该怎么做。
大多数对具有文本的 Qt 元素的请求将 return QString,这是 Qt 使用的简单字符串容器。大多数其他库都需要常规 python 字符串,因此可能需要使用 str() 进行转换。全部:
fromaddr = self.senderEmailLineEdit.text()
toaddrs = self.receiverEmailLineEdit.text()
msg = self.msgTextEdit.toPlainText()
username = self.senderEmailLineEdit.text()
是 QString 对象。
尝试使用内置函数 str() 将变量转换为字符串
只需使用这个:
fromaddr = str(self.senderEmailLineEdit.text())
toaddrs = str(self.receiverEmailLineEdit.text())
msg = str(self.msgTextEdit.toPlainText())
username = str(self.senderEmailLineEdit.text())