PyQt5笔记(04) -- 文本框的使用

来源:互联网 发布:oracle启动数据库命令 编辑:程序博客网 时间:2024/06/07 05:01

本文主要介绍PyQt5中文本框的使用。其中,使用textbox.setText(string) 方法设置文本框内容,使用textbox.text() 获取文本框里面的内容。

import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QAction, QMessageBoxfrom PyQt5.QtGui import QIconfrom PyQt5.QtCore import pyqtSlotfrom PyQt5.Qt import QLineEditclass App(QWidget):    def __init__(self):        super().__init__()        self.title = 'PyQt5 textbox'        self.left = 10        self.top = 10        self.width = 320        self.height = 200        self.initUI()    def initUI(self):        self.setWindowTitle(self.title)        self.setGeometry(self.left, self.top, self.width, self.height)        # create textbox        self.textbox = QLineEdit(self)        self.textbox.move(20, 20)        self.textbox.resize(280, 40)        # Create a button in the window        self.button = QPushButton('show text', self)        self.button.move(20, 80)        # connect button to function on_click        self.button.clicked.connect(self.on_click)        self.show()    @pyqtSlot()    def on_click(self):        textboxValue = self.textbox.text()        QMessageBox.question(self, "Message", 'You typed:' + textboxValue,                              QMessageBox.Ok, QMessageBox.Ok)        """打印完毕之后清空文本框"""        self.textbox.setText('')if __name__ == '__main__':    app = QApplication(sys.argv)    ex = App()    app.exit(app.exec_())