代码语言:javascript复制
本篇介绍PyQt5 的字体选择对话框,颜色选择对话框,打印预览对话框,和打印对话框。
- QFontDialog.getFont()
- QColorDialog.getColor()
- QPrintPreviewDialog()
- QPrintDialog(printer, self)
具体的运用详见代码:
代码语言:javascript复制import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QColor, QFont
from PyQt5.QtCore import Qt
from PyQt5.QtPrintSupport import QPrinter, QPrintDialog,QPrintPreviewDialog
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("PyQt5 颜色/字体/打印相关对话框")
vlayout =QVBoxLayout()
bt1 = QPushButton("字体对话框")
bt2 = QPushButton("颜色对话框(字体颜色)")
bt2_ = QPushButton("字体背景颜色")
bt3 = QPushButton("打印预览对话框")
bt4 = QPushButton("打印对话框")
self.textEdit = QTextEdit()#初始化时换行符无效
self.textEdit.setText("第一行n第二行n第三行n")
self.textEdit.selectAll()
vlayout. addWidget(bt1)
vlayout. addWidget(bt2)
vlayout. addWidget(bt2_)
vlayout. addWidget(bt3)
vlayout. addWidget(bt4)
vlayout. addWidget(self.textEdit)
self.setLayout(vlayout)
bt1.clicked.connect(self.font_setup)
bt2.clicked.connect(self.textcolor_setup)
bt2_.clicked.connect(self.textbgcolor_setup)
bt3.clicked.connect(self.printPreview)
bt4.clicked.connect(self.print_setup)
self.resize(400,400)
def font_setup(self):
font, ok = QFontDialog.getFont()
if ok:
#print((font.family(), font.pointSize(), font.bold(), font.italic(),font.underline(),font.strikeOut()))
print("字体:%s" % font.family())
print("字号:%s" % font.pointSize())
print("是否粗体:%s" % font.bold())
print("是否斜体:%s" % font.italic())
print("是否加下划线:%s" % font.underline())
print("是否加删除线:%s" % font.strikeOut())
self.textEdit.setCurrentFont(font)
'''
QTextEdit 其他 字体设置相关的方法。
font = self.textEdit.currentFont()
self.textEdit.setFontFamily (self, QString fontFamily)
self.textEdit.setFontItalic (self, bool b)
self.textEdit.setFontPointSize (self, float s)
self.textEdit.setFontUnderline (self, bool b)
self.textEdit.setFontWeight (self, int w)
'''
def textcolor_setup(self):
'''
.getColor.(initial: Union[QColor, Qt.GlobalColor, QGradient] = Qt.white, parent: QWidget = None, title: str = '',
options: Union[QColorDialog.ColorDialogOptions, QColorDialog.ColorDialogOption] = QColorDialog.ColorDialogOptions())
'''
color = QColorDialog.getColor()#使用默认参数
if color.isValid():
self.textEdit.setTextColor (color)
print(color.getRgb())#整形 RGBA
print(color.getRgbF())#浮点型 RGBA
'''
.setTextBackgroundColor (self, QColor c)
.setTextColor (self, QColor c)
self.textEdit.textBackgroundColor (self)
self.textEdit.textColor (self)
'''
def textbgcolor_setup(self):
color =QColorDialog.getColor(Qt.white,None, '选择文字背景颜色',QColorDialog.ShowAlphaChannel )
#color =QColorDialog.getColor(Qt.white,None, '选择文字背景颜色',QColorDialog.NoButtons )
print (color.getRgb())
if color.isValid():
self.textEdit.setTextBackgroundColor (color)
def printPreview(self):#打印预览
dialog = QPrintPreviewDialog()
dialog.paintRequested.connect(self.handlePrint)
dialog.exec_()
def print_setup(self):#打印,无预览
printer = QPrinter()
dialog = QPrintDialog(printer, self)
if dialog.exec_() == QDialog.Accepted:
self.handlePrint(printer)
def handlePrint(self, printer):#打印方法
self.textEdit.print(printer)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())