代码拉取完成,页面将自动刷新
# -*- coding:utf-8 -*-
__author__ = 'sunhao'
__email__ = 'sunhao.java@gmail.com'
__date__ = '2015-4-19 05:48'
import sys
import ctypes
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtPrintSupport import QPrintDialog, QPrinter
QtCore.QTextCodec.setCodecForLocale(QtCore.QTextCodec.codecForName("UTF-8"))
class Notepad(QtWidgets.QMainWindow):
def __init__(self):
"""全局变量"""
self.clipboard = QtWidgets.QApplication.clipboard()
QtWidgets.QMainWindow.__init__(self)
self.initUI()
def initUI(self):
self.setWindowTitle("无标题 - 记事本")
self.setWindowIcon(QtGui.QIcon("images/notepad.png"))
# 设置编辑区
self.text = QtWidgets.QTextEdit()
self.setCentralWidget(self.text)
self.createActions()
self.createStatusBar()
self.createMenubars()
self.createToolBars()
self.readSettings()
self.text.document().contentsChanged.connect(self.documentWasModified)
self.setCurrentFile('')
def documentWasModified(self):
self.setWindowModified(self.text.document().isModified())
def readSettings(self):
settings = QtCore.QSettings("sunhao", "notepad")
size = settings.value("size", QtCore.QSize(1000, 600))
screen = QtWidgets.QDesktopWidget().screenGeometry()
pos = settings.value("pos",
QtCore.QPoint((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2))
wrapMode = settings.value("wrapMode", QtWidgets.QTextEdit.WidgetWidth)
fonts = settings.value("fonts", QtGui.QFont("Consolas", 14))
toolbar = settings.value("toolbar", self.toolBar.isHidden())
self.resize(size)
self.move(pos)
self.text.setLineWrapMode(wrapMode)
self.text.setFont(fonts)
if 0 == wrapMode:
self.autoWrapAction.setIcon(QtGui.QIcon("images/check_no.png"))
elif 1 == wrapMode:
self.autoWrapAction.setIcon(QtGui.QIcon("images/check.png"))
if "true" == toolbar:
self.toolBar.hide()
self.toolBarAction.setIcon(QtGui.QIcon("images/check_no.png"))
else:
self.toolBar.show()
self.toolBarAction.setIcon(QtGui.QIcon("images/check.png"))
def writeSettings(self):
settings = QtCore.QSettings("sunhao", "notepad")
settings.setValue("size", self.size())
settings.setValue("pos", self.pos())
settings.setValue("wrapMode", self.text.lineWrapMode())
settings.setValue("fonts", self.text.font())
settings.setValue("toolbar", self.toolBar.isHidden())
def createActions(self):
self.newAction = QtWidgets.QAction(QtGui.QIcon('images/new.png'), "&新建", self,
shortcut=QtGui.QKeySequence.New, statusTip="创建文件",
triggered=self.newFile)
self.openAction = QtWidgets.QAction(QtGui.QIcon('images/open.png'), "&打开...",
self, shortcut=QtGui.QKeySequence.Open,
statusTip="打开文件", triggered=self.openFileEvent)
self.saveAction = QtWidgets.QAction(QtGui.QIcon('images/save.png'), "&保存", self,
shortcut=QtGui.QKeySequence.Save,
statusTip="保存文件", triggered=self.save)
self.saveAsAction = QtWidgets.QAction(QtGui.QIcon('images/save.png'), "另存为...", self,
shortcut=QtGui.QKeySequence.SaveAs,
statusTip="另存文件",
triggered=self.saveAs)
self.printAction = QtWidgets.QAction(QtGui.QIcon('images/print.png'), "打印...", self,
shortcut=QtGui.QKeySequence.Print,
statusTip="打印文件",
triggered=self.printText)
self.exitAction = QtWidgets.QAction(QtGui.QIcon('images/exit.png'), "退出", self, shortcut="Ctrl+Q",
statusTip="退出程序", triggered=self.close)
self.undoAction = QtWidgets.QAction(QtGui.QIcon('images/undo.png'), "撤销", self,
shortcut=QtGui.QKeySequence.Undo,
statusTip="撤销编辑",
triggered=self.text.undo)
self.cutAction = QtWidgets.QAction(QtGui.QIcon('images/cut.png'), "剪切", self,
shortcut=QtGui.QKeySequence.Cut,
statusTip="剪切选中的文本",
triggered=self.text.cut)
self.copyAction = QtWidgets.QAction(QtGui.QIcon('images/copy.png'), "复制", self,
shortcut=QtGui.QKeySequence.Copy,
statusTip="复制选中的文本",
triggered=self.text.copy)
self.pasteAction = QtWidgets.QAction(QtGui.QIcon('images/paste.png'), "粘贴", self,
shortcut=QtGui.QKeySequence.Paste,
statusTip="粘贴剪切板的文本",
triggered=self.text.paste)
self.deleteAction = QtWidgets.QAction(QtGui.QIcon("images/delete.png"), "删除", self,
statusTip="删除选中的文本",
triggered=self.delete)
self.selectAllAction = QtWidgets.QAction(QtGui.QIcon('images/selectAll.png'), "全选", self,
shortcut=QtGui.QKeySequence.SelectAll,
statusTip="全选",
triggered=self.text.selectAll)
self.dateAction = QtWidgets.QAction(QtGui.QIcon("images/date.png"), "时间/日期", self, shortcut="F5",
statusTip="插入时间/日期",
triggered=self.dateEvent)
self.autoWrapAction = QtWidgets.QAction(QtGui.QIcon("images/check.png"), "自动换行", self,
statusTip="设置自动换行",
triggered=self.setWrap)
self.fontAction = QtWidgets.QAction(QtGui.QIcon("images/font.png"), "字体", self,
statusTip="设置字体", triggered=self.setFont_)
self.toolBarAction = QtWidgets.QAction(QtGui.QIcon("images/check.png"), "工具栏", self,
statusTip="状态栏",
triggered=self.toggleToolBar)
self.aboutAction = QtWidgets.QAction(QtGui.QIcon("images/about.png"), "关于", self, triggered=self.about)
self.aboutQtAction = QtWidgets.QAction(QtGui.QIcon("images/qt.png"), "关于Qt", self,
triggered=QtWidgets.QApplication.instance().aboutQt)
self.undoAction.setEnabled(False)
self.cutAction.setEnabled(False)
self.copyAction.setEnabled(False)
self.deleteAction.setEnabled(False)
self.text.undoAvailable.connect(self.undoAction.setEnabled)
self.text.copyAvailable.connect(self.cutAction.setEnabled)
self.text.copyAvailable.connect(self.copyAction.setEnabled)
self.text.copyAvailable.connect(self.deleteAction.setEnabled)
def createStatusBar(self):
self.statusBar().showMessage("准备就绪")
def createMenubars(self):
file = self.menuBar().addMenu("文件")
file.addAction(self.newAction)
file.addAction(self.openAction)
file.addAction(self.saveAction)
file.addAction(self.saveAsAction)
file.addSeparator()
file.addAction(self.printAction)
file.addSeparator()
file.addAction(self.exitAction)
edit = self.menuBar().addMenu("编辑")
edit.addAction(self.undoAction)
edit.addSeparator()
edit.addAction(self.cutAction)
edit.addAction(self.copyAction)
edit.addAction(self.pasteAction)
edit.addAction(self.deleteAction)
edit.addSeparator()
edit.addAction(self.selectAllAction)
edit.addAction(self.dateAction)
style = self.menuBar().addMenu("格式")
style.addAction(self.autoWrapAction)
style.addAction(self.fontAction)
view = self.menuBar().addMenu("查看")
view.addAction(self.toolBarAction)
help = self.menuBar().addMenu("帮助")
help.addAction(self.aboutAction)
help.addAction(self.aboutQtAction)
def createToolBars(self):
self.toolBar = self.addToolBar("")
self.toolBar.addAction(self.newAction)
self.toolBar.addAction(self.openAction)
self.toolBar.addAction(self.saveAction)
self.toolBar.addSeparator()
self.toolBar.addAction(self.cutAction)
self.toolBar.addAction(self.copyAction)
self.toolBar.addAction(self.pasteAction)
def newFile(self):
if self.maybeSave():
self.text.clear()
def maybeSave(self):
if self.text.document().isModified():
ret = self.tip()
if 0 == ret:
return self.save()
if 2 == ret:
return False
return True
def openFileEvent(self):
# 如果先前被打开的文件已被修改,需要提示
if self.maybeSave():
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self)
file = QtCore.QFile(fileName)
if not file.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text):
QtWidgets.QMessageBox.warning(self, "记事本",
"文件%s不能被读取:\n%s." % (fileName, file.errorString()))
return
inf = QtCore.QTextStream(file)
QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)
self.text.setPlainText(inf.readAll())
QtWidgets.QApplication.restoreOverrideCursor()
self.setCurrentFile(fileName)
self.statusBar().showMessage("文件读取成功", 2000)
def setCurrentFile(self, fileName):
self.curFile = fileName
self.text.document().setModified(False)
self.setWindowModified(False)
if self.curFile:
shownName = self.strippedName(self.curFile)
else:
shownName = '未命名.txt'
self.setWindowTitle("%s[*] - 记事本" % shownName)
def strippedName(self, fullFileName):
return QtCore.QFileInfo(fullFileName).fileName()
def save(self):
if self.curFile:
return self.saveFile(self.curFile)
else:
return self.saveAs()
def saveAs(self):
fileName, _ = QtWidgets.QFileDialog.getSaveFileName(self)
if fileName:
return self.saveFile(fileName)
return False
def saveFile(self, fileName):
file = QtCore.QFile(fileName)
if not file.open(QtCore.QFile.WriteOnly | QtCore.QFile.Text):
QtWidgets.QMessageBox.warning(self, "记事本",
"文件%s不能被写入:\n%s." % (fileName, file.errorString()))
return False
outf = QtCore.QTextStream(file)
QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)
outf << self.text.toPlainText()
QtWidgets.QApplication.restoreOverrideCursor()
self.setCurrentFile(fileName)
self.statusBar().showMessage("写入文件成功", 2000)
return True
def closeEvent(self, event):
if not self.maybeSave():
event.ignore()
else:
self.writeSettings()
event.accept()
def tip(self, title="记事本", content="文件已被修改,是否保存?"):
alertBox = QtWidgets.QMessageBox(self)
saveButton = alertBox.addButton("保存", QtWidgets.QMessageBox.ActionRole)
unSaveButton = alertBox.addButton("不保存", QtWidgets.QMessageBox.ActionRole)
cancelButton = alertBox.addButton("取消", QtWidgets.QMessageBox.ActionRole)
alertBox.setWindowTitle(title)
alertBox.setText(content)
alertBox.exec_()
button = alertBox.clickedButton()
if saveButton == button:
return 0
elif unSaveButton == button:
return 1
elif cancelButton == button:
return 2
else:
return -1;
def dateEvent(self):
"""插入时间"""
current = QtCore.QDateTime.currentDateTime();
current = current.toString("yyyy-MM-dd hh:mm");
self.text.insertPlainText(current)
def printText(self):
document = self.text.document()
printer = QPrinter()
dlg = QPrintDialog(printer, self)
if dlg.exec_() != QtWidgets.QDialog.Accepted:
return
document.print_(printer)
self.statusBar().showMessage("打印成功", 2000)
def delete(self):
cursor = self.text.textCursor()
if not cursor.isNull():
cursor.removeSelectedText()
self.statusBar().showMessage("删除成功", 2000)
def setWrap(self):
mode = self.text.lineWrapMode()
if 1 == mode:
# 自动换行
self.text.setLineWrapMode(QtWidgets.QTextEdit.NoWrap)
self.autoWrapAction.setIcon(QtGui.QIcon("images/check_no.png"))
else:
# 不自动换行
self.text.setLineWrapMode(QtWidgets.QTextEdit.WidgetWidth)
self.autoWrapAction.setIcon(QtGui.QIcon("images/check.png"))
def toggleToolBar(self):
if self.toolBar.isHidden():
self.toolBar.show()
self.toolBarAction.setIcon(QtGui.QIcon("images/check.png"))
else:
self.toolBar.hide()
self.toolBarAction.setIcon(QtGui.QIcon("images/check_no.png"))
def setFont_(self):
font, ok = QtWidgets.QFontDialog.getFont(QtGui.QFont(self.text.toPlainText()), self)
if ok:
self.text.setFont(font)
def about(self):
QtWidgets.QMessageBox.about(self, "关于记事本",
"这是仿照Windows系统自带记事本(Nodepad)应用写的一个基于Python_3.4.1 + PyQt_5.4.1的山寨记事本\r\n"
"作者:孙昊\r\n"
"时间:2015-04-18 19:51 ~ 2015-04-19 12:42")
# 设置Windows任务栏的图标
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("myappid")
app = QtWidgets.QApplication(sys.argv)
notepad = Notepad()
notepad.show()
app.exec_()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。