Pythoner集中营PyQt

PyQt5重写回车事件

2018-08-08  本文已影响14人  酉月十一

PyQt5中的事件处理主要依赖重写事件处理函数,下面重写回车事件,代码如下

from __future__ import division
import sys
from math import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Form(QDialog):
    def __init__(self,parent=None):
        super(Form,self).__init__(parent)
        self.browser = QTextBrowser()
        self.lineedit = QLineEdit('press enter')
        self.lineedit.selectAll()
        layout = QVBoxLayout()
        layout.addWidget(self.browser)
        layout.addWidget(self.lineedit)
        self.setLayout(layout)
        self.lineedit.setFocus()
        self.setWindowTitle('重写回车事件')
    #重写回车事件
    def keyPressEvent(self, event):
        try:
            text = self.lineedit.text()
            self.browser.append("%s" % text)
        except:
            self.browser.append("%s is invalid!" % text)
            

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w=Form()
    w.show()
    app.exec_()
上一篇 下一篇

猜你喜欢

热点阅读