2010-07-14 2 views
2

Я пытаюсь сделать свое первое приложение для python. Я хочу создать простую форму отправителя электронной почты. В Qt Designer создать dialog.ui, затем генерировать dialog.py из dialog.ui и написать там функционируютpython Qt4 Как связать действие при нажатии кнопки

def SendEmail(self,efrom,eto,esubj,ebody): 
    msg = MIMEText(ebody.encode('UTF-8'),'html', 'UTF-8') 
    msg['Subject'] = esubj 
    msg['From'] = efrom 
    msg['To'] = eto 
    s = smtplib.SMTP() 
    s.connect("mail.driversoft.net", 25) 
    s.login("[email protected]", "1234567") 
    s.sendmail(efrom, [eto], msg.as_string()) 
    s.quit() 

и попробуйте подключиться к слоту

QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.SendEmail("[email protected]", "[email protected]", "subject", "bodytext")) 

, когда я пытаюсь запустить это приложение Я не вижу форму, на мой адрес электронной почты реанимировать сообщение, а в консоли

Traceback (most recent call last): 
File "C:\Documents and Settings\a.ivanov\My Documents\Aptana Studio Workspace\test1\src\wxtest.py", line 61, in <module> 
ui.setupUi(Dialog) 
File "C:\Documents and Settings\a.ivanov\My Documents\Aptana Studio Workspace\test1\src\wxtest.py", line 33, in setupUi 
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.SendEmail("[email protected]", "[email protected]", "subject", "bodytext")) 
TypeError: arguments did not match any overloaded call: 
QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType' 
QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType' 
QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 2 has unexpected type 'bytes' 

Как я могу сделать форму я Wich я написал предмет, а затем текст нажмите кнопку Отправить и получить сообщение по электронной почте? Спасибо!

полный код

from PyQt4 import QtCore, QtGui 
import smtplib 
from email.mime.text import MIMEText 

class Ui_Dialog(object): 
    def setupUi(self, Dialog): 
     Dialog.setObjectName("Dialog") 
     Dialog.resize(400, 330) 
     self.textEdit = QtGui.QTextEdit(Dialog) 
     self.textEdit.setGeometry(QtCore.QRect(10, 70, 381, 221)) 
     self.textEdit.setObjectName("textEdit") 
     self.subjEdit = QtGui.QLineEdit(Dialog) 
     self.subjEdit.setGeometry(QtCore.QRect(10, 30, 371, 20)) 
     self.subjEdit.setObjectName("subjEdit") 
     self.pushButton = QtGui.QPushButton(Dialog) 
     self.pushButton.setGeometry(QtCore.QRect(100, 300, 75, 23)) 
     self.pushButton.setObjectName("pushButton") 
     self.pushButton_2 = QtGui.QPushButton(Dialog) 
     self.pushButton_2.setGeometry(QtCore.QRect(210, 300, 75, 23)) 
     self.pushButton_2.setObjectName("pushButton_2") 
     self.label = QtGui.QLabel(Dialog) 
     self.label.setGeometry(QtCore.QRect(10, 10, 46, 13)) 
     self.label.setObjectName("label") 
     self.label_2 = QtGui.QLabel(Dialog) 
     self.label_2.setGeometry(QtCore.QRect(11, 53, 46, 13)) 
     self.label_2.setObjectName("label_2") 

     self.retranslateUi(Dialog)   
     QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL("clicked()"), Dialog.close)   
     QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.SendEmail("[email protected]", "[email protected]", "subject", "bodytext")) 
     QtCore.QMetaObject.connectSlotsByName(Dialog) 

#this is my function 
    def SendEmail(self,efrom,eto,esubj,ebody): 
     msg = MIMEText(ebody.encode('UTF-8'),'html', 'UTF-8') 
     msg['Subject'] = esubj 
     msg['From'] = efrom 
     msg['To'] = eto 
     s = smtplib.SMTP() 
     s.connect("mail.driversoft.net", 25) 
     s.login("[email protected]", "1234567") 
     s.sendmail(efrom, [eto], msg.as_string()) 
     s.quit() 
     #print("done") 

    def retranslateUi(self, Dialog): 
     Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8)) 
     self.pushButton.setText(QtGui.QApplication.translate("Dialog", "Send", None, QtGui.QApplication.UnicodeUTF8)) 
     self.pushButton_2.setText(QtGui.QApplication.translate("Dialog", "Close", None, QtGui.QApplication.UnicodeUTF8)) 
     self.label.setText(QtGui.QApplication.translate("Dialog", "Subject", None, QtGui.QApplication.UnicodeUTF8)) 
     self.label_2.setText(QtGui.QApplication.translate("Dialog", "Email text", None, QtGui.QApplication.UnicodeUTF8)) 


if __name__ == "__main__": 
    import sys 
    app = QtGui.QApplication(sys.argv) 
    Dialog = QtGui.QDialog() 
    ui = Ui_Dialog() 
    ui.setupUi(Dialog)  
    Dialog.show() 
    sys.exit(app.exec_()) 

ответ

6
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.SendEmail("[email protected]", "[email protected]", "subject", "bodytext")) 

Это не то, как работает система СИГНАЛ-SLOT. Ваш сигнал «clicked()» не имеет параметров, поэтому ваш слот также не должен иметь никаких параметров. И вы должны передать ссылку на обратный вызов, а не называть эту функцию при подключении.

QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.pushButtonClicked) 

def pushButtonClicked(self): 
    self.SendEmail("[email protected]", "[email protected]", "subject", "bodytext") 
+1

большое спасибо! сейчас он отлично работает! – driver