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的新方法&
import sys from PyQt5.QtWidgets import QApplication, QWidget , QMainWindow from Calui1 import Ui_Form class Mywindow(QWidget,Ui_Form): def __init__(self): super(Mywindow,self).__init__() self.setupUi(self) def btnClear_clicked(self): self.lineEdit.clear() def btn1(self): self.lineEdit.insert("1") def btn2(self): self.lineEdit.insert("2") def btn3(self): self.lineEdit.insert("3") def btn4(self): self.lineEdit.insert("4") def btn5(self): self.lineEdit.insert("5") def btn6(self): self.lineEdit.insert("6") def btn7(self): self.lineEdit.insert("7") def btn8(self): self.lineEdit.insert("8") def btn9(self): self.lineEdit.insert("9") def btn0(self): self.lineEdit.insert("0") def btn_plus(self): self.lineEdit.insert("+") def btn_sub(self): self.lineEdit.insert("-") def btn_mul(self): self.lineEdit.insert("*") def btn_div(self): self.lineEdit.insert("/") def btnequal(self): text=self.lineEdit.text() self.lineEdit.insert("= %.2f" %(eval(text))) if __name__=='__main__': app=QApplication(sys.argv) win=Mywindow() win.pushButton_7.clicked.connect(win.btnClear_clicked()) win.pushButton.clicked.connect(win.btn1()) win.pushButton_2.clicked.connect(win.btn2()) win.pushButton_3.clicked.connect(win.btn3()) win.pushButton_4.clicked.connect(win.btn_plus()) win.pushButton_5.clicked.connect(win.btn4()) win.pushButton_6.clicked.connect(win.btn7()) win.pushButton_8.clicked.connect(win.btn5()) win.pushButton_9.clicked.connect(win.btn8()) win.pushButton_10.clicked.connect(win.btn0()) win.pushButton_11.clicked.connect(win.btn6()) win.pushButton_12.clicked.connect(win.btn9()) win.pushButton_13.clicked.connect(win.btnequal()) win.pushButton_14.clicked.connect(win.btn_sub()) win.pushButton_15.clicked.connect(win.btn_mul()) win.pushButton_16.clicked.connect(win.btn_div()) win.show() sys.exit(app.exec_())错在哪
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值