PyQT5之單行文字輸入到多行顯示窗

星空28發表於2024-06-05

from PyQt5 import QtWidgets
from PyQt5 import QtCore, QtGui
import sys
import cv2


class TextDemoPanel(QtWidgets.QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        select_btn = QtWidgets.QPushButton("輸入")
        self.line_text = QtWidgets.QLineEdit()
        self.line_text.setMinimumWidth(200)
        # self.line_text.setValidator(QtCore.QIntValidator())  # 文字輸入只能是整數

        self.mul_txt_browser = QtWidgets.QTextBrowser()    # 多文字顯示框
        self.mul_txt_browser.setFixedSize(640, 640)
        self.mul_txt_browser.setReadOnly(True)    # 只讀
        self.mul_txt_browser.setStyleSheet("background-color:pink;color:green")  # 背景顏色設定
        font = QtGui.QFont()
        font.setBold(True)
        font.setPointSizeF(18)
        self.mul_txt_browser.setFont(font)

        btn_panel = QtWidgets.QGroupBox("文字輸入")
        hboxlayout = QtWidgets.QHBoxLayout()
        hboxlayout.addWidget(self.line_text)
        hboxlayout.addWidget(select_btn)
        hboxlayout.addStretch(1)
        btn_panel.setLayout(hboxlayout)

        vboxlayout = QtWidgets.QVBoxLayout()
        vboxlayout.addWidget(self.mul_txt_browser)
        vboxlayout.addWidget(btn_panel)
        vboxlayout.addStretch(1)
        self.setLayout(vboxlayout)

        # 繫結點選
        self.line_text.editingFinished.connect(self.on_update_text)

    def on_update_text(self):

        curr_txt = self.line_text.text()
        self.line_text.clear()
        self.mul_txt_browser.append(curr_txt)


if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    main_win = QtWidgets.QMainWindow()
    main_win.setWindowTitle("文字框演示")
    myPanel = TextDemoPanel()
    main_win.setCentralWidget(myPanel)
    # main_win.setMinimumSize(1080, 720)
    main_win.show()
    app.exec_()

相關文章