效果图:
这次添加了关闭-撤销-重做-全选-清空等功能,并添加了字体和字体大小选择。基本方法跟前面几篇类似。
ui_mainWindow.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'ui_mainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(691, 367)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEdit.setGeometry(QtCore.QRect(0, 0, 691, 291))
font = QtGui.QFont()
font.setPointSize(18)
self.plainTextEdit.setFont(font)
self.plainTextEdit.setObjectName("plainTextEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 691, 21))
self.menubar.setObjectName("menubar")
self.menu = QtWidgets.QMenu(self.menubar)
self.menu.setObjectName("menu")
self.menu_2 = QtWidgets.QMenu(self.menubar)
self.menu_2.setObjectName("menu_2")
self.menu_3 = QtWidgets.QMenu(self.menubar)
self.menu_3.setObjectName("menu_3")
MainWindow.setMenuBar(self.menubar)
self.toolBar = QtWidgets.QToolBar(MainWindow)
self.toolBar.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
self.toolBar.setObjectName("toolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.statusBar = QtWidgets.QStatusBar(MainWindow)
self.statusBar.setObjectName("statusBar")
MainWindow.setStatusBar(self.statusBar)
self.actFont_Italic = QtWidgets.QAction(MainWindow)
self.actFont_Italic.setCheckable(True)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("icons/images/Italic.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actFont_Italic.setIcon(icon)
self.actFont_Italic.setObjectName("actFont_Italic")
self.actFont_Bold = QtWidgets.QAction(MainWindow)
self.actFont_Bold.setCheckable(True)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("icons/images/Bold.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actFont_Bold.setIcon(icon1)
self.actFont_Bold.setObjectName("actFont_Bold")
self.actFont_Underline = QtWidgets.QAction(MainWindow)
self.actFont_Underline.setCheckable(True)
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("icons/images/underline.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actFont_Underline.setIcon(icon2)
self.actFont_Underline.setObjectName("actFont_Underline")
self.actEdit_Cut = QtWidgets.QAction(MainWindow)
self.actEdit_Cut.setEnabled(False)
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap("icons/images/cut.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actEdit_Cut.setIcon(icon3)
self.actEdit_Cut.setObjectName("actEdit_Cut")
self.actEdit_Copy = QtWidgets.QAction(MainWindow)
self.actEdit_Copy.setEnabled(False)
icon4 = QtGui.QIcon()
icon4.addPixmap(QtGui.QPixmap("icons/images/copy.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actEdit_Copy.setIcon(icon4)
self.actEdit_Copy.setObjectName("actEdit_Copy")
self.actEdit_Paste = QtWidgets.QAction(MainWindow)
self.actEdit_Paste.setEnabled(False)
icon5 = QtGui.QIcon()
icon5.addPixmap(QtGui.QPixmap("icons/images/paste.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actEdit_Paste.setIcon(icon5)
self.actEdit_Paste.setObjectName("actEdit_Paste")
self.actFile_New = QtWidgets.QAction(MainWindow)
icon6 = QtGui.QIcon()
icon6.addPixmap(QtGui.QPixmap("icons/images/new.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actFile_New.setIcon(icon6)
self.actFile_New.setObjectName("actFile_New")
self.actFile_Open = QtWidgets.QAction(MainWindow)
icon7 = QtGui.QIcon()
icon7.addPixmap(QtGui.QPixmap("icons/images/open.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actFile_Open.setIcon(icon7)
self.actFile_Open.setObjectName("actFile_Open")
self.actFile_Save = QtWidgets.QAction(MainWindow)
icon8 = QtGui.QIcon()
icon8.addPixmap(QtGui.QPixmap("icons/images/save.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actFile_Save.setIcon(icon8)
self.actFile_Save.setObjectName("actFile_Save")
self.actClose = QtWidgets.QAction(MainWindow)
icon9 = QtGui.QIcon()
icon9.addPixmap(QtGui.QPixmap("icons/images/close.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actClose.setIcon(icon9)
self.actClose.setObjectName("actClose")
self.actEdit_Undo = QtWidgets.QAction(MainWindow)
self.actEdit_Undo.setEnabled(False)
icon10 = QtGui.QIcon()
icon10.addPixmap(QtGui.QPixmap("icons/images/undo.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actEdit_Undo.setIcon(icon10)
self.actEdit_Undo.setObjectName("actEdit_Undo")
self.acEdit_Redo = QtWidgets.QAction(MainWindow)
self.acEdit_Redo.setEnabled(False)
icon11 = QtGui.QIcon()
icon11.addPixmap(QtGui.QPixmap("icons/images/redo.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.acEdit_Redo.setIcon(icon11)
self.acEdit_Redo.setObjectName("acEdit_Redo")
self.actEdit_Clear = QtWidgets.QAction(MainWindow)
icon12 = QtGui.QIcon()
icon12.addPixmap(QtGui.QPixmap("icons/images/clear.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actEdit_Clear.setIcon(icon12)
self.actEdit_Clear.setObjectName("actEdit_Clear")
self.actEdit_SelectAll = QtWidgets.QAction(MainWindow)
self.actEdit_SelectAll.setObjectName("actEdit_SelectAll")
self.actLang_CN = QtWidgets.QAction(MainWindow)
self.actLang_CN.setCheckable(True)
icon13 = QtGui.QIcon()
icon13.addPixmap(QtGui.QPixmap("icons/images/chinese.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actLang_CN.setIcon(icon13)
self.actLang_CN.setObjectName("actLang_CN")
self.actLang_EN = QtWidgets.QAction(MainWindow)
self.actLang_EN.setCheckable(True)
icon14 = QtGui.QIcon()
icon14.addPixmap(QtGui.QPixmap("icons/images/English.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actLang_EN.setIcon(icon14)
self.actLang_EN.setObjectName("actLang_EN")
self.menu.addAction(self.actFile_New)
self.menu.addAction(self.actFile_Open)
self.menu.addAction(self.actFile_Save)
self.menu.addSeparator()
self.menu.addAction(self.actClose)
self.menu_2.addAction(self.actEdit_Cut)
self.menu_2.addAction(self.actEdit_Copy)
self.menu_2.addAction(self.actEdit_Paste)
self.menu_2.addSeparator()
self.menu_2.addAction(self.actEdit_Undo)
self.menu_2.addAction(self.acEdit_Redo)
self.menu_2.addSeparator()
self.menu_2.addAction(self.actEdit_SelectAll)
self.menu_2.addAction(self.actEdit_Clear)
self.menu_3.addAction(self.actFont_Italic)
self.menu_3.addAction(self.actFont_Bold)
self.menu_3.addAction(self.actFont_Underline)
self.menu_3.addSeparator()
self.menubar.addAction(self.menu.menuAction())
self.menubar.addAction(self.menu_2.menuAction())
self.menubar.addAction(self.menu_3.menuAction())
self.toolBar.addAction(self.actFile_New)
self.toolBar.addAction(self.actFile_Open)
self.toolBar.addAction(self.actFile_Save)
self.toolBar.addSeparator()
self.toolBar.addAction(self.actEdit_Cut)
self.toolBar.addAction(self.actEdit_Copy)
self.toolBar.addAction(self.actEdit_Paste)
self.toolBar.addSeparator()
self.toolBar.addAction(self.actEdit_Undo)
self.toolBar.addAction(self.acEdit_Redo)
self.toolBar.addSeparator()
self.toolBar.addAction(self.actFont_Italic)
self.toolBar.addAction(self.actFont_Bold)
self.toolBar.addAction(self.actFont_Underline)
self.toolBar.addSeparator()
self.toolBar.addAction(self.actLang_CN)
self.toolBar.addAction(self.actLang_EN)
self.toolBar.addSeparator() self.retranslateUi(MainWindow)
self.actEdit_Cut.triggered.connect(self.plainTextEdit.cut)
self.actEdit_Copy.triggered.connect(self.plainTextEdit.copy)
self.actEdit_Paste.triggered.connect(self.plainTextEdit.paste)
self.actClose.triggered.connect(MainWindow.close)
self.actEdit_Undo.triggered.connect(self.plainTextEdit.undo)
self.acEdit_Redo.triggered.connect(self.plainTextEdit.redo)
self.actEdit_Clear.triggered.connect(self.plainTextEdit.clear)
self.actEdit_SelectAll.triggered.connect(self.plainTextEdit.selectAll)
self.plainTextEdit.undoAvailable['bool'].connect(self.actEdit_Undo.setEnabled)
self.plainTextEdit.redoAvailable['bool'].connect(self.acEdit_Redo.setEnabled)
QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menu.setTitle(_translate("MainWindow", "文件(F)"))
self.menu_2.setTitle(_translate("MainWindow", "编辑(E)"))
self.menu_3.setTitle(_translate("MainWindow", "格式(M)"))
self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
self.actFont_Italic.setText(_translate("MainWindow", "斜体"))
self.actFont_Italic.setToolTip(_translate("MainWindow", "<html><head/><body><p><span style=\" font-style:italic;\">斜体</span></p></body></html>"))
self.actFont_Bold.setText(_translate("MainWindow", "粗体"))
self.actFont_Bold.setToolTip(_translate("MainWindow", "加粗"))
self.actFont_Underline.setText(_translate("MainWindow", "下划线"))
self.actFont_Underline.setToolTip(_translate("MainWindow", "下划线"))
self.actEdit_Cut.setText(_translate("MainWindow", "剪切"))
self.actEdit_Cut.setToolTip(_translate("MainWindow", "剪切到粘贴板"))
self.actEdit_Cut.setShortcut(_translate("MainWindow", "Ctrl+X"))
self.actEdit_Copy.setText(_translate("MainWindow", "复制"))
self.actEdit_Copy.setToolTip(_translate("MainWindow", "复制到粘贴板"))
self.actEdit_Copy.setShortcut(_translate("MainWindow", "Ctrl+C"))
self.actEdit_Paste.setText(_translate("MainWindow", "粘贴"))
self.actEdit_Paste.setToolTip(_translate("MainWindow", "从粘贴板粘贴"))
self.actEdit_Paste.setShortcut(_translate("MainWindow", "Ctrl+V"))
self.actFile_New.setText(_translate("MainWindow", "新建"))
self.actFile_New.setToolTip(_translate("MainWindow", "新建文件"))
self.actFile_New.setShortcut(_translate("MainWindow", "Ctrl+N"))
self.actFile_Open.setText(_translate("MainWindow", "打开..."))
self.actFile_Open.setToolTip(_translate("MainWindow", "打开文件"))
self.actFile_Open.setShortcut(_translate("MainWindow", "Ctrl+O"))
self.actFile_Save.setText(_translate("MainWindow", "保存"))
self.actFile_Save.setToolTip(_translate("MainWindow", "保存修改"))
self.actFile_Save.setShortcut(_translate("MainWindow", "Ctrl+S"))
self.actClose.setText(_translate("MainWindow", "关闭"))
self.actClose.setToolTip(_translate("MainWindow", "关闭本窗口"))
self.actEdit_Undo.setText(_translate("MainWindow", "撤销"))
self.actEdit_Undo.setToolTip(_translate("MainWindow", "撤销上次编辑操作"))
self.actEdit_Undo.setShortcut(_translate("MainWindow", "Ctrl+Z"))
self.acEdit_Redo.setText(_translate("MainWindow", "重做"))
self.acEdit_Redo.setToolTip(_translate("MainWindow", "重做上次操作"))
self.acEdit_Redo.setShortcut(_translate("MainWindow", "Ctrl+Y"))
self.actEdit_Clear.setText(_translate("MainWindow", "清空"))
self.actEdit_Clear.setToolTip(_translate("MainWindow", "清空"))
self.actEdit_SelectAll.setText(_translate("MainWindow", "全选"))
self.actEdit_SelectAll.setToolTip(_translate("MainWindow", "全选"))
self.actEdit_SelectAll.setShortcut(_translate("MainWindow", "Ctrl+A"))
self.actLang_CN.setText(_translate("MainWindow", "汉语"))
self.actLang_CN.setToolTip(_translate("MainWindow", "汉语界面"))
self.actLang_EN.setText(_translate("MainWindow", "英语"))
self.actLang_EN.setToolTip(_translate("MainWindow", "英语"))
myMainWindow.py
#!/usr/bin/env python
# _*_ coding: UTF-8 _*_
"""=================================================
@Project -> File : Operate-system -> myMainWindow.py
@IDE : PyCharm
@Author : zihan
@Date : 2020/4/11 14:44
@Desc :
=================================================""" import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QActionGroup, QLabel, QProgressBar, QSpinBox, QFontComboBox)
from PyQt5.QtCore import Qt, pyqtSlot
from PyQt5.QtGui import QTextCharFormat, QFont from ui_mainWindow import Ui_MainWindow class QmyMainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self) self.__buildUI() # 设置斜体
self.ui.actFont_Italic.triggered.connect(self.do_act_font_italic_triggered)
# 设置粗体
self.ui.actFont_Bold.triggered.connect(self.do_act_font_bold_triggered)
# 设置下划线
self.ui.actFont_Underline.triggered.connect(self.do_act_underline_triggered) # 新建文件
self.ui.actFile_New.triggered.connect(self.do_act_file_new_triggered)
# 打开文件
self.ui.actFile_Open.triggered.connect(self.do_act_file_open_triggered)
# 保存文件
self.ui.actFile_Save.triggered.connect(self.do_act_file_save_triggered) # 设置字体大小
self.__spinFontSize.valueChanged[int].connect(self.do_fontsize_changed)
# 字体选择
self.__comboFontName.currentIndexChanged[str].connect(self.do_fontname_changed) # 文本框内容可复制
self.ui.plainTextEdit.copyAvailable.connect(self.do_plain_textedit_copyavailable)
# 文本选择内容发生改变
self.ui.plainTextEdit.selectionChanged.connect(self.do_plain_textedit_selectionchanged)
# 标准右键菜单
self.ui.plainTextEdit.customContextMenuRequested.connect(self.do_plain_textedit_customctmnreq) self.setCentralWidget(self.ui.plainTextEdit) def __buildUI(self): # 窗体上动态添加组件
# 创建状态栏上的组件
self.__LabFile = QLabel(self) # QLabel组件显示信息
self.__LabFile.setMinimumWidth(150)
self.__LabFile.setText("文件名:")
self.ui.statusBar.addWidget(self.__LabFile) # 添加到状态栏 self.__progressBar1 = QProgressBar(self)
self.__progressBar1.setMaximumWidth(200)
self.__progressBar1.setMinimum(5)
self.__progressBar1.setMaximum(50)
sz = self.ui.plainTextEdit.font().pointSize() # 字体大小
self.__progressBar1.setValue(sz)
self.ui.statusBar.addWidget(self.__progressBar1) # 添加到状态栏 self.__LabInfo = QLabel(self) # QLabel组件显示字体名称
self.__LabInfo.setText("选择字体名称:")
self.ui.statusBar.addPermanentWidget(self.__LabInfo) # 添加到状态栏 # 为actLang_CN和actLang_EN创建QActionGroup,互斥型选择
actionGroup = QActionGroup(self)
actionGroup.addAction(self.ui.actLang_CN)
actionGroup.addAction(self.ui.actLang_EN)
actionGroup.setExclusive(True) # 互斥型分组
self.ui.actLang_CN.setChecked(True) # 创建工具我是谁,我在哪儿栏上的组件
self.__spinFontSize = QSpinBox(self) # 字体大小spinBox
self.__spinFontSize.setMinimum(5)
self.__spinFontSize.setMaximum(50)
sz = self.ui.plainTextEdit.font().pointSize()
self.__spinFontSize.setValue(sz)
self.__spinFontSize.setMinimumWidth(50)
self.ui.toolBar.addWidget(self.__spinFontSize) # 添加到工具栏 self.__comboFontName = QFontComboBox(self) # 字体comboBox
self.__comboFontName.setMinimumWidth(100)
self.ui.toolBar.addWidget(self.__comboFontName) # 添加到工具栏 self.ui.toolBar.addSeparator() # 添加一个分隔条
self.ui.toolBar.addAction(self.ui.actClose) # 添加一个关闭按钮 @pyqtSlot(bool)
def do_act_font_italic_triggered(self, checked): # 斜体
fmt = self.ui.plainTextEdit.currentCharFormat()
fmt.setFontItalic(checked)
self.ui.plainTextEdit.mergeCurrentCharFormat(fmt) @pyqtSlot(bool)
def do_act_font_bold_triggered(self, checked): # 粗体
fmt = self.ui.plainTextEdit.currentCharFormat()
if checked:
fmt.setFontWeight(QFont.Bold)
else:
fmt.setFontWeight(QFont.Normal)
self.ui.plainTextEdit.mergeCurrentCharFormat(fmt) @pyqtSlot(bool)
def do_act_underline_triggered(self, checked): # 下划线
fmt = self.ui.plainTextEdit.currentCharFormat()
fmt.setFontUnderline(checked)
self.ui.plainTextEdit.mergeCurrentCharFormat(fmt) def do_act_file_new_triggered(self): # 新建文件,不实现具体功能
self.__LabFile.setText(" 新建文件") def do_act_file_open_triggered(self): # 打开文件,不实现具体功能
self.__LabFile.setText(" 打开的文件") def do_act_file_save_triggered(self): # 保存文件,不实现具体功能
self.__LabFile.setText(" 文件已保存") @pyqtSlot(int)
def do_fontsize_changed(self, fontSize): # 设置字体大小
fmt = self.ui.plainTextEdit.currentCharFormat()
fmt.setFontPointSize(fontSize)
self.ui.plainTextEdit.mergeCurrentCharFormat(fmt)
self.__progressBar1.setValue(fontSize) @pyqtSlot(str)
def do_fontname_changed(self, fontName): # 设置字体
fmt = self.ui.plainTextEdit.currentCharFormat()
fmt.setFontFamily(fontName)
self.ui.plainTextEdit.mergeCurrentCharFormat(fmt)
self.__LabInfo.setText("字体名称:%s " % fontName) # 文本内容可复制
def do_plain_textedit_copyavailable(self, avi):
self.ui.actEdit_Cut.setEnabled(avi)
self.ui.actEdit_Copy.setEnabled(avi)
self.ui.actEdit_Paste.setEnabled(self.ui.plainTextEdit.canPaste()) # 文本选择内容发生改变时
def do_plain_textedit_selectionchanged(self):
fmt = self.ui.plainTextEdit.currentCharFormat()
self.ui.actFont_Bold.setChecked(fmt.font().bold())
self.ui.actFont_Italic.setChecked(fmt.fontItalic())
self.ui.actFont_Underline.setChecked(fmt.fontUnderline()) # 标准右键菜单
def do_plain_textedit_customctmnreq(self, pos):
popMenu = self.ui.plainTextEdit.createStandardContextMenu()
popMenu.exec(pos) # 显示快捷菜单 if __name__ == "__main__":
app = QApplication(sys.argv) # 创建app,用QApplication类
form = QmyMainWindow()
form.show()
sys.exit(app.exec_())
特别提醒一点的是可以自己在Property里面设置剪切-复制-粘贴等的初始状态disable,只需要去掉Enable那个属性的勾就好了。至于怎么使它Enable,对于PlainTextEdit控件来说,有一个槽函数copyAvailable可以设置他们的使能。另外那个customContextMenuRequested(pos)函数在这个程序里面还没发现什么作用,没有什么特别的功能,如果有人发现了,可以告诉我一下,哈哈哈。