2014-07-11 77 views
-1

我想在Python中使電子郵件客戶端。即時得到以下錯誤:PyQt4如何從QTextEdit和無法創建文本的文本創建消息框

def egonder(self): 
    server = smtplib.SMTP('smtp.gmail.com', 587) 
    server.ehlo() 
    server.starttls() 
    usrnam=self.ad.text() 
    passwds=self.sifre.text() 
    if usrnam=="" or passwds=="": 
     QtGui.QMessageBox.warning("Fill every blank.") 
    else: 
     server.login(usrnam,passwds) 
     self.durum.setText("Logging in...") 
     mytext="" 
     self.mesaj.setPlainText(mytext) 
     msg = self.konu.text(),"\n",mytext 
     server.sendmail(self.kimden.text(), self.kime.text(), msg) 
     self.durum.setText("Email sent.") 

它給了我以下錯誤:

File "eposta.py", line 105, in egonder 
    server.sendmail(self.kimden.text(), self.kime.text(), msg) 
    File "/usr/lib/python3.4/smtplib.py", line 793, in sendmail 
    (code, resp) = self.data(msg) 
    File "/usr/lib/python3.4/smtplib.py", line 532, in data 
    q = _quote_periods(msg) 
    File "/usr/lib/python3.4/smtplib.py", line 168, in _quote_periods 
    return re.sub(br'(?m)^\.', b'..', bindata) 
    File "/usr/lib/python3.4/re.py", line 175, in sub 
    return _compile(pattern, flags).sub(repl, string, count) 
TypeError: expected string or buffer 

,當我嘗試使消息框顯示它給我的錯誤:

QtGui.QMessageBox.warning("Fill every blank.") 
TypeError: arguments did not match any overloaded call: 
    QMessageBox.warning(QWidget, str, str, QMessageBox.StandardButtons  buttons=QMessageBox.Ok, QMessageBox.StandardButton defaultButton=QMessageBox.NoButton): argument 1 has unexpected type 'str' 
QMessageBox.warning(QWidget, str, str, int, int, int button2=0): argument 1 has unexpected type 'str' 
QMessageBox.warning(QWidget, str, str, str, str button1Text='', str button2Text='', int defaultButtonNumber=0, int escapeButtonNumber=-1): argument 1 has unexpected type 'str' 

謝謝

回答

1

錯誤1

您的線路:msg = self.konu.text(),"\n",mytext

這是創建一個字符串列表。如果你print它,你會看到類似這樣的:

('Something', '\n', 'Something More') 

SMTP.sendmail要求對消息的字符串。因此,嘗試這個:

msg = "{0}\n{1}".format(self.konu.text(), mytext) 

請閱讀文檔的SMTP.sendmailstring.format

錯誤2

您還沒有經過QMessageBox提示所有必需的參數。至少它需要消息框的父級,標題和消息。所以,如果沒有父:

QtGui.QMessageBox.warning(None, "Invalid input", "Fill every blank.") 

請閱讀docs

+0

非常感謝您! – user3755771

+0

@ user3755771如果已經爲您排序,請接受我的回答。 – Shadow9043

+0

對不起@ Shadow9040不知道我應該這樣做。完成 – user3755771

-1

OK:

QtGui.QMessageBox.warning(None, "Invalid input", "Fill every blank.") 
+3

雖然此代碼可能回答此問題,但提供 關於_why_和/或_how_的附加上下文將回答 問題將顯着改善其長期值 的值。請[編輯]你的答案,添加一些解釋。 –

+0

這不是一個答案。它只是複製在接受答案末尾給出的代碼,似乎只不過是一個「適合我」的評論。 – ekhumoro