欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pyqt QPlainTextEdit 中捕獲回車的示例代碼

 更新時間:2024年03月14日 15:59:34   作者:計算機輔助工程  
在PyQt的QPlainTextEdit控件中,可以通過重寫keyPressEvent()函數(shù)來捕獲鍵盤事件,這篇文章主要介紹了pyqt QPlainTextEdit 中捕獲回車,需要的朋友可以參考下

pyqt QPlainTextEdit 中捕獲回車

在PyQt的QPlainTextEdit控件中,可以通過重寫keyPressEvent()函數(shù)來捕獲鍵盤事件。當按下回車鍵時,會發(fā)送一個Key_Return信號,我們可以連接這個信號到自定義的槽函數(shù)上進行處理。

以下是示例代碼:

from PyQt5.QtWidgets import QApplication, QMainWindow, QPlainTextEdit
from PyQt5.QtCore import Qt
import sys
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.plaintextedit = QPlainTextEdit(self)
        self.setCentralWidget(self.plaintextedit)
        # 連接鍵盤事件信號與槽函數(shù)
        self.plaintextedit.keyPressEvent = self.handle_keypress
    def handle_keypress(self, event):
        if event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter:
            print("捕獲了回車鍵")
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

運行該程序后,每次在QPlainTextEdit中按下回車鍵時,都會輸出"捕獲了回車鍵"。

補充:

PyQt5_QPlainTextEdit_多行純文本編輯器

QPlainTextEdit多行純文本編輯器

  • 描述
  • QPlainText和QTextEdit大致功能實現(xiàn)差不多,但針對純文本處理進行了優(yōu)化
  • 與QTextEdit相同
    • 適用于段落和字符
      • 段落是一個格式化的字符串,為了適應(yīng)控件的寬度, 會自動換行
      • 默認情況下,在讀取純文本時,一個換行符表示一個段落。
      • 文檔由零個或多個段落組成。段落由硬線斷開分隔。
      • 段落中的每個字符都有自己的屬性,例如字體和顏色。
    • 內(nèi)容的編輯
      • 文本的選擇由QTextCursor類處理,該類提供創(chuàng)建選擇,檢索文本內(nèi)容或刪除選擇的功能
      • QPlainTextEdit包含一個QTextDocument對象,可以使用document()方法檢索該對象
    • 與QTextEdit的差異
      • QPlainTextEdit是一個簡略版本的類(使用QTextEdit和QTextDocument作為背后實現(xiàn)的技術(shù)支撐)
      • 它的性能優(yōu)于QTextEdit, 主要是因為在文本文檔中使用QPlainTextDocumentLayout簡化文本布局
      • 純文本文檔布局不支持表格或嵌入框架,并使用逐行逐段滾動方法替換像素精確高度計算。
  • 繼承自:QAbstractScrollArea
  • 功能作用
QPlainTextEdit(parent: QWidget = None)          # 創(chuàng)建對象的同時設(shè)置父對象
QPlainTextEdit(str, parent: QWidget = None)     # 創(chuàng)建對象的同時添加提示文本和設(shè)置父對象

占位提示文本

setPlaceholderText(str)                         # 設(shè)置占位提示文本
placeholderText() -> str                        # 獲取占位提示文本

只讀設(shè)置

setReadOnly(bool)                               # 設(shè)置是否只讀
isReadOnly() -> bool                            # 判斷是否只讀

格式設(shè)置

currentCharFormat() -> QTextCharFormat          # 獲取當前字符格式
setCurrentCharFormat(QTextCharFormat)           # 設(shè)置當前字符格式
mergeCurrentCharFormat(QTextCharFormat)         # 合并當前字符格式
# 補充
QTextCharFormat
    描述
        提供了一種字符格式信息
        文檔中文本的字符格式指定文本的可視屬性,以及有關(guān)其在超文本文檔中的角色的信息
    設(shè)置字體格式
        setFont(QFont)                                  # 通過QFont對象統(tǒng)一設(shè)置字體格式
        font() -> QFont                                 # 獲取字體格式的QFont對象
        setFontFamily(family_str)                       # 設(shè)置字體家族
        fontFamily() -> str                             # 獲取字體家族
        setFontPointSize(float)                         # 設(shè)置字體大小
        fontPointSize() -> float                        # 獲取字體大小
        setFontWeight(int)                              # 設(shè)置字體粗細
        fontWeight() -> int                             # 獲取字體粗細
        setFontOverline(bool)                           # 設(shè)置字體上劃線
        fontOverline() -> bool                          # 獲取字體是否設(shè)置上劃線
        setFontStrikeOut(bool)                          # 設(shè)置字體中劃線(刪除線)
        fontStrikeOut() -> bool                         # 獲取字體是否設(shè)置中劃線
        setFontUnderline(bool)                          # 獲取字體下劃線
        fontUnderline() -> bool                         # 獲取字體是否設(shè)置下劃線
    字體大小寫
        setFontCapitalization(QFont.Capitalization)     # 設(shè)置字體大小寫格式
        fontCapitalization() -> QFont.Capitalization    # 獲取字體大小寫格式
            # 參數(shù):QFont.Capitalization
                    QFont.MixedCase                     # 正常的文本呈現(xiàn)
                    QFont.AllUppercase                  # 以全大寫類型呈現(xiàn)的文本
                    QFont.AllLowercase                  # 以全小寫類型呈現(xiàn)的文本
                    QFont.SmallCaps                     # 以小型大寫字母呈現(xiàn)的文本
                    QFont.Capitalize                    # 單詞的首字符為大寫字符
    顏色
        setForeground(QColor(100, 200, 150))            # 通過QColor對象設(shè)置字體顏色
    超鏈接
        setAnchorHref("http://www.itlike.com")          # 設(shè)置超鏈接
        anchorHref() -> str                             # 獲取超鏈接

軟換行模式

setLineWrapMode(QPlainTextEdit.LineWrapMode)    # 設(shè)置軟換行模式
lineWrapMode() -> QPlainTextEdit.LineWrapMode   # 獲取軟換行模式
# 補充
QPlainTextEdit.LineWrapMode
     QPlainTextEdit.NoWrap                   # 沒有軟換行
     QPlainTextEdit.WidgetWidth              # 超出控件寬度進行自動換行
  • 覆蓋模式

只能是單個字符操作才可以覆蓋,中文不能覆蓋

setOverwriteMode(bool)                          # 設(shè)置是否覆蓋模式
overwriteMode() -> bool                         # 獲取是否覆蓋模式

Tab控制

setTabChangesFocus(bool)                        # 設(shè)置Tab鍵是否為切換焦點
tabChangesFocus() -> bool                       # 獲取Tab鍵是否為切換焦點
setTabStopDistance(distance_float)              # 設(shè)置一個Tab鍵的寬度
tabStopDistance() -> float                      # 獲取一個Tab鍵的寬度

文本操作

setPlainText(text_str)                          # 設(shè)置普通文本內(nèi)容
insertPlainText(text_str)                       # 插入普通文本
appendPlainText(text_str)                       # 追加普通文本
appendHtml(html_str)                            # 追加HTML字符串
                                                # 注意:有些標簽不支持(表格、列表、圖片、...)
toPlainText()                                   # 獲取文本內(nèi)容,轉(zhuǎn)換成純文本

文本塊數(shù)操作

限制當前文本編輯器中的文本塊個數(shù)(其實就是限制文本段落個數(shù))

blockCount() -> int                             # 獲取當前編輯器中文本塊個數(shù)(段落數(shù))
maximumBlockCount() -> int                      # 獲取當前編輯器最大文本塊個數(shù)(段落數(shù))
setMaximumBlockCount(int)                       # 設(shè)置當前編輯器可以輸入最大文本塊個數(shù)(段落數(shù))
# 當編輯器不設(shè)置最大文本塊個數(shù)時,獲取到最大文本塊數(shù)為0
# 如果輸入內(nèi)容的段落超過最大段落數(shù),則會自動刪除最前面的段落

常用編輯操作

selectAll()                                     # 選中所有
copy()                                          # 復(fù)制選中文本
cut()                                           # 剪切選中文本
paste()                                         # 粘貼文本
canPaste() -> bool                              # 判定是否可以粘貼
clear()                                         # 清空內(nèi)容
redo()                                          # 重做
isUndoRedoEnabled() -> bool                     # 判定撤銷重做是否可用
setUndoRedoEnabled(bool)                        # 設(shè)置撤銷重做是否可用
undo()                                          # 撤銷
find(str, QTextDocument.FindFlags) -> bool      # 查找指定內(nèi)容
    QTextDocument.FindBackward                  # 向后搜索而不是向前搜索。
    QTextDocument.FindCaseSensitively           # 區(qū)分大小寫的查找操作。
    QTextDocument.FindWholeWords                # 使查找匹配僅完整的單詞。
zoomIn(int range = 1)                           # 設(shè)置放大縮小
            range > 0                           # 放大
            range < 0                           # 縮小
zoomOut(int range = 1)                          # 已過期,效果和上面的方法相反

滾動(定位光標行)

centerCursor()                              # 光標所在行定位到編輯器中間位置
ensureCursorVisible()                       # 光標所在行不在編輯范圍時,將光標行定位到頂部/底部
setCenterOnScroll(bool)                     # 傳遞True,表示, 控制光標(文本最后一行),顯示時能夠展示在中間位置
                                            # 注意:必須事先設(shè)置好,其實就是在后面添加一些空文本塊
centerOnScroll() -> bool                    # 獲取是否設(shè)置最后一行光標能滾動到中間位置

光標設(shè)置

textCursor() -> QTextCursor                 # 獲取文本光標對象
cursorForPosition(QPoint) -> QTextCursor    # 獲取指定位置的文本光標對象
cursorWidth() -> int                        # 獲取文本光標對象的寬度
setCursorWidth(int)                         # 設(shè)置文本光標對象的寬度
cursorRect() -> QRect                       # 獲取文本光標對象的矩形
cursorRect(QTextCursor)                     # 獲取指定光標對象的矩形
moveCursor(QTextCursor.MoveOperation,QTextCursor.MoveMode)     # 移動文本光標
    # 參數(shù)
    QTextCursor.MoveOperation
        QTextCursor.NoMove                      # 將光標保持在原位
        QTextCursor.Start                       # 移至文檔的開頭。
        QTextCursor.StartOfLine                 # 移動到當前行的開頭。
        QTextCursor.StartOfBlock                # 移動到當前塊的開頭。
        QTextCursor.StartOfWord                 # 移動到當前單詞的開頭。
        QTextCursor.PreviousBlock               # 移動到上一個塊的開頭。
        QTextCursor.PreviousCharacter           # 移至上一個字符。
        QTextCursor.PreviousWord                # 移到上一個單詞的開頭。
        QTextCursor.Up                          # 向上移動一行。
        QTextCursor.Left                        # 向左移動一個字符。
        QTextCursor.WordLeft                    # 向左移動一個單詞。
        QTextCursor.End                         # 移到文檔的末尾。
        QTextCursor.EndOfLine                   # 移動到當前行的末尾。
        QTextCursor.EndOfWord                   # 移到當前單詞的末尾。
        QTextCursor.EndOfBlock                  # 移動到當前塊的末尾。
        QTextCursor.NextBlock                   # 移動到下一個塊的開頭。
        QTextCursor.NextCharacter               # 移動到下一個角色。
        QTextCursor.NextWord                    # 轉(zhuǎn)到下一個單詞。
        QTextCursor.Down                        # 向下移動一行。
        QTextCursor.Right                       # 向右移動一個角色。
        QTextCursor.WordRight                   # 向右移動一個單詞。
    QTextCursor.MoveMode
        QTextCursor.MoveAnchor                  # 將錨點移動到與光標本身相同的位置。
        QTextCursor.KeepAnchor                  # 將錨固定在原處。

可用信號

textChanged()                                   # 文本改變時
selectionChanged()                              # 選中內(nèi)容改變時
modificationChanged(bool)                       # 編輯狀態(tài)改變時,發(fā)送是否是編輯狀態(tài)
cursorPositionChanged()                         # 光標位置改變時
blockCountChanged(int)                          # 塊的個數(shù)發(fā)生改變時,發(fā)送塊的個數(shù)
updateRequest(QRect rect, int dy)               # 內(nèi)容更新請求時
# 文本框內(nèi)內(nèi)容太多時,滾動滾動條時候,文本框顯示的內(nèi)容發(fā)生改變,那么就需要重新繪制,重新請求當前需要展示什么內(nèi)容
# 此時就會觸發(fā)更新請求信號,此時會對更新區(qū)域矩形、垂直方向滾動步長進行發(fā)送
copyAvailable(bool)                             # 復(fù)制可用時
redoAvailable(bool)                             # 重做可用時
undoAvailable(bool)                             # 撤銷可用時

代碼示例

示例1:創(chuàng)建和功能作用

from PyQt5.Qt import *
import sys
class Windows(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('QPlainTextEdit-創(chuàng)建使用')
        self.resize(500, 500)
        self.widget_list()
    def widget_list(self):
        self.add_widget()
        self.占位提示文本()
        # self.只讀設(shè)置()
        # self.格式設(shè)置()
        # self.軟換行()
        # self.覆蓋模式()
        # self.Tab鍵設(shè)置()
        # self.文本操作()
        # self.文本操作區(qū)別()
        # self.文本塊數(shù)操作()
        # self.常用操作_放大縮小()
        self.滾動操作()
    def add_widget(self):
        pte1 = QPlainTextEdit(self)
        # pte2 = QPlainTextEdit('提示文本', self)
        pte2 = QPlainTextEdit(self)
        pte1.resize(480, 200)
        pte1.move(10, 20)
        pte2.resize(480, 200)
        pte2.move(pte1.x(), pte1.y()+pte1.height()+10)
        btn = QPushButton(self)
        btn.move(pte2.x(), pte2.y()+pte2.height()+10)
        btn.setVisible(False)
        self.pte1 = pte1
        self.pte2 = pte2
        self.btn = btn
    def 占位提示文本(self):
        self.pte1.setPlaceholderText('請輸入你的個人信息')
        self.pte2.setPlaceholderText('請輸入相關(guān)內(nèi)容!')
    def 只讀設(shè)置(self):
        self.pte1.setPlainText('我喜歡Python ' * 2)
        self.pte2.setPlainText('我喜歡Python ' * 2)
        self.pte1.setReadOnly(True)
    def 格式設(shè)置(self):
        tcf = QTextCharFormat()
        tcf.setFontPointSize(20)
        tcf.setFontFamily('華文行楷')
        tcf.setToolTip('提示信息')
        self.pte1.setCurrentCharFormat(tcf)
        self.pte2.setCurrentCharFormat(tcf)
        tcf2=QTextCharFormat()
        tcf2.setFontOverline(True)
        tcf2.setFontUnderline(True)
        self.pte1.setCurrentCharFormat(tcf2)
        self.pte2.mergeCurrentCharFormat(tcf2)
    def 軟換行(self):
        self.pte1.setLineWrapMode(QPlainTextEdit.NoWrap)
        self.pte2.setLineWrapMode(QPlainTextEdit.WidgetWidth)
        self.pte1.setPlainText('沒有軟換行 ' * 20)
        self.pte2.setPlainText('自動軟換行 ' * 20)
    def 覆蓋模式(self):
        self.pte1.setOverwriteMode(True)
        print(self.pte1.overwriteMode())
        print(self.pte2.overwriteMode())
    def Tab鍵設(shè)置(self):
        self.pte1.setTabChangesFocus(True)
        self.pte2.setTabStopDistance(160)
    def 文本操作(self):
        self.btn.setVisible(True)
        self.btn.setText('文本操作')
        self.btn.resize(100, 30)
        self.i = 0
        def text_oper():
            self.i += 1
            if self.i == 1:
                self.pte1.insertPlainText('插入普通文本')
            elif self.i == 2:
                self.pte1.appendPlainText('追加普通文本')
            else:
                self.pte1.appendHtml('<a  rel="external nofollow" >追加HTML文本</a>')
                self.i = 0
        self.btn.clicked.connect(text_oper)
        pass
    def 文本操作區(qū)別(self):
        # 首先添加一個QTextEdit對象,用來進行對比
        te = QTextEdit(self)
        te.resize(480, 200)
        # 放置在pte2上面
        te.move(self.pte1.x(), self.pte1.y() + self.pte1.height() + 10)
        self.btn.setVisible(True)
        self.btn.setText('插入表格')
        self.btn.resize(100, 30)
        table_str = """
        <table border=1>
            <tr>
                <td>A1</td>
                <td>B1</td>
                <td>C1</td>
            </tr>
            <tr>
                <td>A2</td>
                <td>B2</td>
                <td>C2</td>
            </tr>
        </table>
        """
        # table_str = '<table><tr><td>A1</td></tr></table>'
        def append_table():
            self.pte1.insertPlainText('這是QPlainTextEdit對象')
            te.insertPlainText('這是QTextEdit對象')
            self.pte1.appendHtml(table_str)
            te.insertHtml(table_str)
        self.btn.clicked.connect(append_table)
    def 文本塊數(shù)操作(self):
        self.btn.setVisible(True)
        self.btn.setText('獲取文本塊數(shù)')
        self.btn.resize(150, 30)
        def test():
            n = self.pte1.blockCount()
            m = self.pte1.maximumBlockCount()
            self.pte2.insertPlainText('上面文本編輯器當前段落數(shù):' + str(n) + '\n')
            self.pte2.insertPlainText('上面文本編輯器最大段落數(shù):' + str(m) +'\n')
            self.pte1.setMaximumBlockCount(3)
        self.btn.clicked.connect(test)
    def 常用操作_放大縮小(self):
        self.pte1.zoomIn(5)
        self.pte2.zoomIn(-2)
    def 滾動操作(self):
        self.pte1.appendPlainText('定位光標所在行\(zhòng)n' * 50)
        def btn_test():
            self.pte1.setCenterOnScroll(True)       # 設(shè)置可以將最后一行定位到中間位置
            self.pte1.centerCursor()                # 將光標所在行定位到中間
            # self.pte1.ensureCursorVisible()       # 光標所在行超出編輯范圍時,將光標所在行定位到頂部/底部
            self.pte1.setFocus()
        self.btn.setVisible(True)
        self.btn.resize(150, 30)
        self.btn.setText('定位光標')
        self.btn.clicked.connect(btn_test)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Windows()
    window.show()
    sys.exit(app.exec_())

示例2:光標操作

from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
window = QWidget()
window.resize(500, 550)
window.setWindowTitle('QPlainTextEdit-光標操作')
pte = QPlainTextEdit(window)
te = QTextEdit(window)
pte.resize(480, 200)
pte.move(10, 10)
te.resize(480,200)
te.move(pte.x(), pte.y()+pte.height()+10)
tc = pte.textCursor()
tc2 = te.textCursor()
def insert_img():
    pte.clear()
    te.clear()
    img = QTextImageFormat()
    img.setName('../images/jpg/1.jpg')
    img.setWidth(460)
    img.setHeight(180)
    tc.insertImage(img)
    tc2.insertImage(img)
def insert_tab():
    pte.clear()
    te.clear()
    tbf = QTextTableFormat()
    tbf.setBorder(2)
    tbf.setWidth(200)
    tbf.setBackground(QColor(255,0,0))
    tc.insertTable(3, 5, tbf)
    tc2.insertTable(3, 5, tbf)
def insert_text():
    pte.clear()
    te.clear()
    tc.insertText('插入文本內(nèi)容 \n' * 20 )
    tc2.insertText('插入文本內(nèi)容 \n' * 20 )
def get_cursor():
    pte.clear()
    te.clear()
    pte.insertPlainText('獲取指定坐標位置的光標\n' * 20)
    # 獲取指定坐標位置的光標對象
    new_tc = pte.cursorForPosition(QPoint(100, 80))
    new_tc.insertText('在光標位置插入內(nèi)容')
def cursor_width():
    pte.setCursorWidth(10)
    te.setCursorWidth(20)
    pte.setFocus()
    te.setFocus()
def move_cursor():
    pte.clear()
    pte.insertPlainText('插入文本內(nèi)容\n' * 50)
    # 移動光標到開頭位置,并且錨點固定在原處
    pte.moveCursor(QTextCursor.Start, QTextCursor.KeepAnchor)
    # 移動光標到下一行,并且錨點跟隨移動到光標位置
    pte.moveCursor(QTextCursor.Down, QTextCursor.MoveAnchor)
    pte.setFocus()
btn = QPushButton(window)
btn.move(te.x(),te.y()+te.height()+10)
btn.resize(150,30)
btn.setText('插入文本')
btn.clicked.connect(insert_text)
btn1 = QPushButton(window)
btn1.resize(150,30)
btn1.move(btn.x() + btn.width() + 10,btn.y())
btn1.setText('插入圖片')
btn1.clicked.connect(insert_img)
btn2 = QPushButton(window)
btn2.resize(150,30)
btn2.move(btn1.x() + btn1.width() + 10,btn1.y())
btn2.setText('插入表格')
btn2.clicked.connect(insert_tab)
btn3 = QPushButton(window)
btn3.resize(150,30)
btn3.move(btn.x(),btn.y() + btn.height()+10)
btn3.setText('獲取光標')
btn3.clicked.connect(get_cursor)
btn4 = QPushButton(window)
btn4.resize(150,30)
btn4.move(btn1.x(),btn1.y()+btn1.height()+10)
btn4.setText('光標寬度')
btn4.clicked.connect(cursor_width)
btn5 = QPushButton(window)
btn5.resize(150,30)
btn5.move(btn2.x(),btn2.y() + btn2.height()+10)
btn5.setText('移動光標')
btn5.clicked.connect(move_cursor)
window.show()
sys.exit(app.exec_())

示例3:QPlainTextEdit可用信號1

from PyQt5.Qt import *
import sys
class Windows(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('QPlainTextEdit-可用信號')
        self.resize(500, 500)
        self.widget_list()
    def widget_list(self):
        self.add_widget()
    def textchanged_btn(self):
        self.pte.textChanged.connect(lambda : print('文本內(nèi)容發(fā)生了改變'))
    def selection_changed(self):
        tc = self.pte.textCursor()          # 獲取文本光標
        text = tc.selectedText()            # 通過文本光標獲取當前選中文本內(nèi)容
        print('當前選中內(nèi)容:', text)
    def selectionChanged_btn(self):
        # self.pte.selectionChanged.connect(lambda : print('選擇的內(nèi)容發(fā)生了改變'))
        # 通過文本光標,可用獲取到當前選中的內(nèi)容
        self.pte.selectionChanged.connect(self.selection_changed)
    def modification_Changed(self, val):
        if val:
            print('當前處于編輯狀態(tài)', val)
        # 后期可以判斷用戶按下了保存,再設(shè)置文本文檔的編輯狀態(tài)為False
        doc = self.pte.document()
        doc.setModified(False)
    def modificationChanged_btn(self):
        self.pte.modificationChanged.connect(self.modification_Changed)
    def cursorPositionChanged_btn(self):
        self.pte.cursorPositionChanged.connect(lambda :print('光標位置發(fā)生了改變'))
    def blockCount_Changed(self, i):
        print('新增了一個段落,當前段落是第【' + str(i) + '】段')
    def blockCountChanged_btn(self):
        self.pte.blockCountChanged.connect(self.blockCount_Changed)
    def add_widget(self):
        pte = QPlainTextEdit(self)      # 實例化QPlainTextEdit對象
        self.pte = pte
        pte.resize(480, 200)
        pte.move(10, 10)
        frame = QFrame(self)
        frame.resize(480, 200)
        frame.move(pte.x(), pte.y() + pte.height()+10)
        for i in range(1, 4):
            for j in range(1, 4):
                btn = QPushButton(frame)
                btn.resize(150, 30)
                btn.setObjectName(str(i)+str(j))
                # print(btn.objectName())
                btn.move((btn.width() + 10) * (j - 1), (btn.height()  + 10) * (i - 1))
        frame.findChild(QPushButton,'11').setText('文本內(nèi)容改變信號')
        frame.findChild(QPushButton,'11').clicked.connect(self.textchanged_btn)
        frame.findChild(QPushButton,'12').setText('選中內(nèi)容改變信號')
        frame.findChild(QPushButton,'12').clicked.connect(self.selectionChanged_btn)
        frame.findChild(QPushButton, '13').setText('編輯狀態(tài)改變信號')
        frame.findChild(QPushButton, '13').clicked.connect(self.modificationChanged_btn)
        frame.findChild(QPushButton, '21').setText('光標位置改變信號')
        frame.findChild(QPushButton, '21').clicked.connect(self.cursorPositionChanged_btn)
        frame.findChild(QPushButton, '22').setText('塊的個數(shù)改變信號')
        frame.findChild(QPushButton, '22').clicked.connect(self.blockCountChanged_btn)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Windows()
    window.show()
    sys.exit(app.exec_())

示例4:QPlainTextEdit可用信號2

模擬行號根據(jù)內(nèi)容增加而移動

from PyQt5.Qt import *
import sys
class Windows(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('QPlainTextEdit-可用信號2')
        self.resize(500, 500)
        self.widget_list()
        self.old_count = 0
    def widget_list(self):
        self.add_widget()
    def update_rows(self):
        max_num = self.pte.blockCount() + 20
        num_list = '\n'.join([str(i) for i in range(1, max_num)])
        self.labe.setText(num_list)
        self.labe.adjustSize()   # 設(shè)置標簽大小跟隨文本內(nèi)容(跟隨內(nèi)容)改變
        self.labe.setAlignment(Qt.AlignRight)  # 設(shè)置文本內(nèi)容居右排列
    def update_Request(self, str, i):
        self.labe.move(0, self.labe.y() + i)
        if self.old_count == self.pte.blockCount():
            return None
        self.update_rows()
        self.old_count = self.pte.blockCount()
    def add_widget(self):
        pte = QPlainTextEdit(self)
        self.pte = pte
        pte.resize(450, 300)
        pte.move(40, 10)
        num_parent = QWidget(self)
        num_parent.resize(30, 300)
        num_parent.move(10, 10)
        num_parent.setStyleSheet('background-color: #d4d4d4;')
        self.labe = QLabel(num_parent)
        self.labe.move(0, 5)
        self.update_rows()
        pte.updateRequest.connect(self.update_Request)      # QPlainTextEdit內(nèi)容更新事件
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Windows()
    window.show()
    sys.exit(app.exec_())

到此這篇關(guān)于pyqt QPlainTextEdit 中捕獲回車的示例代碼的文章就介紹到這了,更多相關(guān)pyqt QPlainTextEdit 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 對python實現(xiàn)模板生成腳本的方法詳解

    對python實現(xiàn)模板生成腳本的方法詳解

    今天小編就為大家分享一篇對python實現(xiàn)模板生成腳本的方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python讀取excel文件中帶公式的值的實現(xiàn)

    Python讀取excel文件中帶公式的值的實現(xiàn)

    這篇文章主要介紹了Python讀取excel文件中帶公式的值的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Python基本文件操作實用指南

    Python基本文件操作實用指南

    文件操作是我們開發(fā)中必不可少的一項需求,下面這篇文章主要給大家介紹了關(guān)于Python基本文件操作的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-06-06
  • Python 文件操作的詳解及實例

    Python 文件操作的詳解及實例

    這篇文章主要介紹了Python 文件操作的詳解及實例的相關(guān)資料,希望通過本文大家能夠理解掌握Python 文件操作的知識,需要的朋友可以參考下
    2017-09-09
  • python如何實現(xiàn)一個刷網(wǎng)頁小程序

    python如何實現(xiàn)一個刷網(wǎng)頁小程序

    這篇文章主要給大家介紹了關(guān)于利用python如何實現(xiàn)一個刷網(wǎng)頁小程序的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • python3.7 利用函數(shù)os pandas利用excel對文件名進行歸類

    python3.7 利用函數(shù)os pandas利用excel對文件名進行歸類

    這篇文章主要介紹了python3.7 利用函數(shù)os pandas利用excel對文件名進行歸類,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 吳恩達機器學(xué)習(xí)練習(xí):神經(jīng)網(wǎng)絡(luò)(反向傳播)

    吳恩達機器學(xué)習(xí)練習(xí):神經(jīng)網(wǎng)絡(luò)(反向傳播)

    這篇文章主要介紹了學(xué)習(xí)吳恩達機器學(xué)習(xí)中的一個練習(xí):神經(jīng)網(wǎng)絡(luò)(反向傳播),在這個練習(xí)中,你將實現(xiàn)反向傳播算法來學(xué)習(xí)神經(jīng)網(wǎng)絡(luò)的參數(shù),需要的朋友可以參考下
    2021-04-04
  • 用代碼幫你了解Python基礎(chǔ)(3)

    用代碼幫你了解Python基礎(chǔ)(3)

    這篇文章主要用代碼幫你了解Python基礎(chǔ),使用循環(huán),字典和集合的示例代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 在Python中利用Pandas庫處理大數(shù)據(jù)的簡單介紹

    在Python中利用Pandas庫處理大數(shù)據(jù)的簡單介紹

    這篇文章簡單介紹了在Python中利用Pandas處理大數(shù)據(jù)的過程,Pandas庫的使用能夠很好地展現(xiàn)數(shù)據(jù)結(jié)構(gòu),是近來Python項目中經(jīng)常被使用使用的熱門技術(shù),需要的朋友可以參考下
    2015-04-04
  • 用pandas劃分數(shù)據(jù)集實現(xiàn)訓(xùn)練集和測試集

    用pandas劃分數(shù)據(jù)集實現(xiàn)訓(xùn)練集和測試集

    這篇文章主要介紹了用pandas劃分數(shù)據(jù)集實現(xiàn)訓(xùn)練集和測試集,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評論