PyQT5之单行文本输入到多行显示窗


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

posted @ 2024-06-05 07:06  星空28  阅读(15)  评论(0编辑  收藏  举报