PyQt5筆記(04) -- 文本框的使用


目錄

PyQt5筆記(01) – 創建空白窗體
PyQt5筆記(02) – 按鈕點擊事件
PyQt5筆記(03) – 消息框
PyQt5筆記(04) – 文本框的使用
PyQt5筆記(05) – 絕對位置
為了便於后期更新,所有目錄已匯總到一個鏈接,具體請移步到這里

正文

本文主要介紹PyQt5中文本框的使用。其中,使用textbox.setText(string) 方法設置文本框內容,使用textbox.text() 獲取文本框里面的內容。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QAction, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
from PyQt5.Qt import QLineEdit

class 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_())


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM