self.connect(self.lineedit, SIGNAL("returnPressed()"), self.updateUi)换成PyQt5该怎么写?

在PyQt5中已经废弃了SINGAL()和SLOT()的调用方式

PyQt4下这种写法:

self.connect(self.lineedit, SIGNAL("returnPressed()"), self.updateUi)

换成PyQt5:

self.lineedit.returnPressed.connect(self.updateUi)

 

完整的代码如下:

from math import *
from PyQt5.QtWidgets import QApplication,QDialog,QTextBrowser,QLineEdit,QVBoxLayout
class Form(QDialog):#继承QDialog获得空白表单form
    def __init__(self, parent=None):#父类为None,变成顶级窗口
        super(Form, self).__init__(parent)#初始化
        self.browser = QTextBrowser()#文本框实例化
        self.lineedit = QLineEdit("Type an expression and press Enter")#初始文本
        self.lineedit.selectAll()#初始化是全选中
        layout = QVBoxLayout()
        layout.addWidget(self.browser)#加进去窗口部件
        layout.addWidget(self.lineedit)#加进去窗口部件
        self.setLayout(layout)#布局进行设置
        self.lineedit.setFocus()#设置光标位置在lineedit
        self.lineedit.returnPressed.connect(self.updateUi)#pytq5的新方法,lineedit响应按下去动作,立即调用updateUi方法
        self.setWindowTitle("Calculate")#设置标题
    def updateUi(self):
        try:
            text = self.lineedit.text()
            self.browser.append("{} = {}".format(text,
                                eval(text)))
        except:
            self.browser.append("{} is invalid!"
                                .format(text))
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

你可能感兴趣的:(self.connect(self.lineedit, SIGNAL("returnPressed()"), self.updateUi)换成PyQt5该怎么写?)