【PyQt5】{16} —— QTextEdit文本编辑框(制作一个简易的记事本)

QTextEdit

QTextEdit类提供了一个部件,用于编辑和显示纯文本和富文本。


QTextEdit的一些方法和属性:

  • toPlainText():获取文本
  • setText():设置文本
  • textChanged:文本改变信号(在文本每次发生改变时发射)
  • clear():清空文本
  • setPlaceholderText():设置占位字符串(只有在文本编辑框中没有任何文本时才会显示)

一个简易的记事本:
# -*- coding: utf-8 -*-
"""
Created on Sat May  9 16:17:57 2020

@author: Giyn
"""

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QTextEdit, QVBoxLayout, QHBoxLayout, QMessageBox
from PyQt5.QtGui import QIcon


class Simple_Window(QWidget):
    def __init__(self):
        super(Simple_Window, self).__init__() # 使用super函数可以实现子类使用父类的方法
        self.setWindowTitle("记事本")
        self.setWindowIcon(QIcon('NoteBook.png')) # 设置窗口图标
        self.resize(412, 412)
        self.text_edit = QTextEdit(self) # 实例化一个QTextEdit对象
        self.text_edit.setText("Hello World") # 设置编辑框初始化时显示的文本
        self.text_edit.setPlaceholderText("Please edit text here") # 设置占位字符串
        self.text_edit.textChanged.connect(lambda: print("text is changed!")) # 判断文本是否发生改变
        
        self.save_button = QPushButton("Save", self)
        self.clear_button = QPushButton("Clear", self)
        
        self.save_button.clicked.connect(lambda: self.button_slot(self.save_button))
        self.clear_button.clicked.connect(lambda: self.button_slot(self.clear_button))
        
        self.h_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        
        self.h_layout.addWidget(self.save_button)
        self.h_layout.addWidget(self.clear_button)
        self.v_layout.addWidget(self.text_edit)
        self.v_layout.addLayout(self.h_layout)
        
        self.setLayout(self.v_layout)

    def button_slot(self, button):
        if button == self.save_button:
            choice = QMessageBox.question(self, "Question", "Do you want to save it?", QMessageBox.Yes | QMessageBox.No)
            if choice == QMessageBox.Yes:
                with open('First text.txt', 'w') as f:
                    f.write(self.text_edit.toPlainText())
                self.close()
            elif choice == QMessageBox.No:
                self.close()
        elif button == self.clear_button:
            self.text_edit.clear()
        
        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Simple_Window()
    window.show()
    sys.exit(app.exec())

Output:


When clicking the “Clear”:
>>> text is changed!

When changing the text:
>>> text is changed!
>>> text is changed!
>>> text is changed!
>>> text is changed!
>>> text is changed!
>>> text is changed!
>>> text is changed!
>>> text is changed!
>>> text is changed!
>>> text is changed!
>>> text is changed!
>>> text is changed!

When clicking the “Save”:
After clicking “Yes”:

在这里插入图片描述
Then the program close.


If clicking “No”, the program will close directly.

Reference:https://study.163.com/course/courseLearn.htm?courseId=1208995818

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在 PyQt5 的文本编辑器中添加修改字体的框,可以使用 QFontComboBox 组件。以下是一个简单的示例代码,可以参考一下: ```python from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QFontComboBox, QToolBar, QAction from PyQt5.QtGui import QFont class TextEditor(QMainWindow): def __init__(self): super().__init__() # 创建一个 QTextEdit 组件 self.text_edit = QTextEdit() self.setCentralWidget(self.text_edit) # 创建一个 QFontComboBox 组件 font_combo_box = QFontComboBox() font_combo_box.currentFontChanged.connect(self.on_font_changed) # 创建一个工具栏,将 QFontComboBox 添加到其中 toolbar = QToolBar() toolbar.addWidget(font_combo_box) self.addToolBar(toolbar) # 创建一个 QAction,用于设置字体加粗 bold_action = QAction("Bold", self) bold_action.setCheckable(True) bold_action.triggered.connect(self.on_bold_triggered) # 将 QAction 添加到工具栏上 toolbar.addAction(bold_action) def on_font_changed(self, font): # 当字体改变时,将 QTextEdit 的字体设置为当前选中的字体 self.text_edit.setCurrentFont(font) def on_bold_triggered(self, checked): # 当加粗按钮被按下时,将 QTextEdit 的字体加粗 font = self.text_edit.currentFont() font.setBold(checked) self.text_edit.setCurrentFont(font) if __name__ == '__main__': app = QApplication([]) window = TextEditor() window.show() app.exec_() ``` 该示例中,我们创建了一个 TextEditor 类,继承自 QMainWindow。在 TextEditor 的构造函数中,我们创建了一个 QTextEdit 组件,并将其设置为中央窗口部件。然后,我们创建了一个 QFontComboBox 组件,并将其添加到一个工具栏中。当用户在 QFontComboBox 中选择了一个字体时,我们通过 `currentFontChanged` 信号捕获该事件,然后将 QTextEdit 的字体设置为当前选中的字体。我们还创建了一个 QAction,用于设置字体加粗。当用户点击该按钮时,我们通过 `triggered` 信号捕获该事件,然后将 QTextEdit 的字体加粗。最后,我们将 QAction 添加到工具栏中。 这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值