datetime:2019/5/23 10:44
author:nzb
(带选择的)消息框提示框
import sys
from PyQt5.QtWidgets import QFileDialog, QApplication, QMainWindow, QAction, QTextEdit, QFontDialog, QColorDialog, QMessageBox
from PyQt5 import QtGui,QtCore
from PyQt5.QtPrintSupport import QPrintDialog, QPrinter, QPrintPreviewDialog
from PyQt5.QtCore import QFileInfo
class UI_demo(QMainWindow):
"""用户界面"""
def __init__(self):
super().__init__()
self.title = 'PyQt5 Messagebox'
self.left = 600
self.top = 200
self.width = 500
self.height = 400
self.iconName = '../img/home.ico'
self.initWindow()
def initWindow(self):
self.setWindowIcon(QtGui.QIcon(self.iconName))
self.setGeometry(self.left, self.top, self.width, self.height)
self.setWindowTitle(self.title)
self.CreateMenu()
self.createEditor()
self.show()
def CreateMenu(self):
"""菜单栏"""
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('File')
editMenu = mainMenu.addMenu('Edit')
viewMenu = mainMenu.addMenu('View')
helpMenu = mainMenu.addMenu('Help')
copyAction = QAction(QtGui.QIcon('../img/copy.ico'), 'Copy', self)
copyAction.setShortcut("Ctrl+C")
editMenu.addAction(copyAction)
cutAction = QAction(QtGui.QIcon('../img/cut.png'), 'Cut', self)
cutAction.setShortcut("Ctrl+X")
editMenu.addAction(cutAction)
saveAction = QAction(QtGui.QIcon('../img/save.png'), 'Save', self)
saveAction.setShortcut("Ctrl+S")
editMenu.addAction(saveAction)
printAction = QAction(QtGui.QIcon('../img/print.png'), "Print", self)
printAction.triggered.connect(self.printDialog)
fileMenu.addAction(printAction)
printpreviewAction = QAction(QtGui.QIcon('../img/printpreview.png'), "PrintPreview", self)
printpreviewAction.triggered.connect(self.printPreviewDialog)
fileMenu.addAction(printpreviewAction)
pdfAction = QAction(QtGui.QIcon('../img/pdf.png'), 'PDF', self)
pdfAction.triggered.connect(self.pdfExport)
fileMenu.addAction(pdfAction)
exitAction = QAction(QtGui.QIcon('../img/exit.png'), 'Exit', self)
exitAction.setShortcut("Ctrl+E")
exitAction.triggered.connect(self.exitWindow)
fileMenu.addAction(exitAction)
pasteAction = QAction(QtGui.QIcon('../img/paste.png'), 'Paste', self)
pasteAction.setShortcut("Ctrl+E")
editMenu.addAction(pasteAction)
fontAction = QAction(QtGui.QIcon('../img/font.png'), "Font", self)
fontAction.setShortcut("Ctrl+F")
fontAction.triggered.connect(self.fontDialog)
viewMenu.addAction(fontAction)
colorAction = QAction(QtGui.QIcon('../img/color.png'), "Color", self)
colorAction.triggered.connect(self.colorDialog)
viewMenu.addAction(colorAction)
helpAction = QAction(QtGui.QIcon('../img/about.ico'), "About", self)
helpAction.triggered.connect(self.AboutMessageBox)
helpMenu.addAction(helpAction)
choiceAction = QAction(QtGui.QIcon('../img/about.ico'), "Choice Message", self)
choiceAction.triggered.connect(self.choiceMessageBox)
helpMenu.addAction(choiceAction)
toolbar = self.addToolBar("Toolbar")
toolbar.addAction(copyAction)
toolbar.addAction(cutAction)
toolbar.addAction(saveAction)
toolbar.addAction(exitAction)
toolbar.addAction(pasteAction)
toolbar.addAction(fontAction)
toolbar.addAction(colorAction)
toolbar.addAction(printAction)
toolbar.addAction(pdfAction)
toolbar.addAction(helpAction)
def exitWindow(self):
"""关闭窗口"""
self.close()
def createEditor(self):
"""文档编辑"""
self.textEdit = QTextEdit(self)
self.setCentralWidget(self.textEdit)
def fontDialog(self):
"""字体对话框"""
font, ok = QFontDialog.getFont()
if ok:
self.textEdit.setFont(font)
def colorDialog(self):
"""颜色对话框"""
color = QColorDialog.getColor()
self.textEdit.setTextColor(color)
def printDialog(self):
"""打印文本框"""
printer = QPrinter(QPrinter.HighResolution)
dialog = QPrintDialog(printer, self)
if dialog.exec_() == QPrintDialog.Accepted:
self.textEdit.print_(printer)
def printPreviewDialog(self):
"""打印预览"""
printer = QPrinter(QPrinter.HighResolution)
previewDialog = QPrintPreviewDialog(printer, self)
previewDialog.paintRequested.connect(self.printPreview)
previewDialog.exec_()
def printPreview(self, printer):
"""打印预览"""
self.textEdit.print_(printer)
def pdfExport(self):
"""导出PDF"""
fn, _ = QFileDialog.getSaveFileName(self, "Export PDF", None, "PDF files (.pdf);;All Files()")
if fn != '':
if QFileInfo(fn).suffix() == "":
fn += '.pdf'
printer = QPrinter(QPrinter.HighResolution)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(fn)
self.textEdit.document().print_(printer)
def AboutMessageBox(self):
"""消息框提示框"""
message = QMessageBox.about(self, "About application", "this is simple texteditor application")
def choiceMessageBox(self):
"""带选择的消息框和提示框"""
message = QMessageBox.question(self, "Choice Message", 'Do you like PyQt5?',
QMessageBox.Yes | QMessageBox.No)
if message == QMessageBox.Yes:
self.textEdit.setText("Yes I like PyQt5")
else:
self.textEdit.setText("No I don't like PyQt5")
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = UI_demo()
sys.exit(app.exec_())