from PyQt5.QtWidgets import QWidget, QApplication, QColorDialog, QFontDialog, QTextEdit, QFileDialog
from PyQt5.QtWidgets import QMainWindow, QMessageBox, QDialog, QLineEdit, QPushButton, QHBoxLayout, QVBoxLayout
from PyQt5.QtGui import QIcon, QTextDocument
from PyQt5.QtCore import QDir, QFile
import sys
class NotePad(QMainWindow):
fileName = "./newFile.txt"
def __init__(self):
super(NotePad, self).__init__()
self.setGeometry(75, 75, 250, 430)
self.setWindowTitle("myNotePad")
self.setWindowIcon(QIcon("./image/gif/536717.gif"))
self.textEdit = QTextEdit(self)
self.setCentralWidget(self.textEdit)
self.createMenu()
self.show()
def createMenu(self):
self.fileMenu()
self.editMenu()
self.formatMenu()
self.helpMenu()
pass
def fileMenu(self):
menu = self.menuBar().addMenu("文件(&F)")
newfile = menu.addAction("新建(&N)", self.newFile_triggered)
openfile = menu.addAction("打开(&O)", self.openFile_triggered)
savefile = menu.addAction("保存(&S)", self.saveFile_triggered)
savefileAs = menu.addAction("另存为(&A)...", self.savaFileAs_triggered)
exitfile = menu.addAction("退出(&X)", self.exit_triggered)
newfile. setShortcut("Ctrl+N")
openfile.setShortcut("Ctrl+O")
savefile.setShortcut("Ctrl+S")
def editMenu(self):
menu = self.menuBar().addMenu("编辑(&E)")
revokeEdit = menu.addAction("撤销(&U)", self.revoke_triggered)
recoveryEdit = menu.addAction("恢复(&R)", self.recovery_triggered)
cutEdit = menu.addAction("剪切(&T)", self.cut_triggered)
copyEdit = menu.addAction("复制(&C)", self.copy_triggered)
pasteEdit = menu.addAction("粘贴(&P)", self.paste_triggered)
delEdit = menu.addAction("删除(&L)", self.del__triggered)
findEdit = menu.addAction("查找(&F)", self.find_triggered)
findNextEdit = menu.addAction("查找下一个(&N)")
replaceEdit = menu.addAction("替换(&E)...")
gotoEdit = menu.addAction("转到(&D)...")
checkAllEdit = menu.addAction("全选(&A)")
revokeEdit.setShortcut("Ctrl+Z")
recoveryEdit.setShortcut("Ctrl+Shift+Z")
cutEdit.setShortcut("Ctrl+X")
copyEdit.setShortcut("Ctrl+C")
pasteEdit.setShortcut("Ctrl+V")
delEdit.setShortcut("Del")
findEdit.setShortcut("Ctrl+F")
findNextEdit.setShortcut("F3")
replaceEdit.setShortcut("Ctrl+H")
gotoEdit.setShortcut("Ctrl+G")
checkAllEdit.setShortcut("Ctrl+A")
def formatMenu(self):
self.menuBar().addMenu("Format(&O)")
pass
def helpMenu(self):
self.menuBar().addMenu("Help(&H)")
pass
def newFile_triggered(self):
self.textEdit.setText('')
self.fileName = "./newFile.txt"
def openFile_triggered(self):
filename, fileType = QFileDialog.getOpenFileName(self, "打开文件", './', "Image Files(*.jpg *.png *.txt *.py)")
if len(fileType) != 0:
self.fileName = filename
text = ' '
with open(self.fileName, 'r', encoding='utf-8') as f:
for line in f.readlines():
text += line
self.textEdit.setPlainText(text)
def saveFile_triggered(self):
with open(self.fileName, 'w', encoding='utf-8') as f:
text = self.textEdit.toPlainText()
f.writelines(text)
def savaFileAs_triggered(self):
filename, fileType = QFileDialog.getSaveFileName(self, '保存文件')
if len(fileType) != 0:
with open(filename, 'w', encoding='utf-8') as f:
text = self.textEdit.toPlainText()
f.writelines(text)
def exit_triggered(self):
self.close()
def revoke_triggered(self):
self.textEdit.undo()
def recovery_triggered(self):
self.textEdit.redo()
def cut_triggered(self):
self.textEdit.cut()
def copy_triggered(self):
self.textEdit.copy()
def paste_triggered(self):
self.textEdit.paste()
def del__triggered(self):
self.textEdit.deleteLater()
def find_triggered(self):
findDlg = QDialog(self)
findDlg.setWindowTitle('查找...')
self.find_textLineEdit = QLineEdit(findDlg)
find_next_button = QPushButton('查找下一个', findDlg)
find_last_button = QPushButton('查找上一个', findDlg)
v_layout = QVBoxLayout(self)
v_layout.addWidget(find_last_button)
v_layout.addWidget(find_next_button)
h_layout = QHBoxLayout(findDlg)
h_layout.addWidget(self.find_textLineEdit)
h_layout.addLayout(v_layout)
find_last_button.clicked.connect(self.show_findLast)
find_next_button.clicked.connect(self.show_findNext)
findDlg.show()
def show_findLast(self):
find_text = self.find_textLineEdit.text()
print(find_text)
print(self.textEdit.find(find_text, QTextDocument.FindBackward))
def show_findNext(self):
find_text = self.find_textLineEdit.text()
print(find_text)
print(self.textEdit.find(find_text, QTextDocument.FindBackward))
if self.textEdit.find(find_text, QTextDocument.FindBackward):
QMessageBox.warning(self, '查找', '找不到 {}'.format(find_text))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = NotePad()
sys.exit(app.exec_())