码迷,mamicode.com
首页 > 其他好文 > 详细

打印机

时间:2021-06-02 18:22:15      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:led   none   printer   打开   begin   encoding   mamicode   filename   qt5   

使用打印机

‘‘‘

使用打印机



‘‘‘

from PyQt5 import QtGui, QtWidgets, QtPrintSupport
from PyQt5.QtWidgets import *
import sys

class PrintSupport(QMainWindow):
    def __init__(self):
        super(PrintSupport,self).__init__()
        self.setGeometry(500, 200, 300, 300)
        self.button = QPushButton(‘打印QTextEdit控件中的内容‘,self)
        self.button.setGeometry(20,20,260,30)
        self.editor = QTextEdit(‘默认文本‘,self)
        self.editor.setGeometry(20,60,260,200)

        self.button.clicked.connect(self.print)

    def print(self):
        printer = QtPrintSupport.QPrinter()

        painter = QtGui.QPainter()
        # 将绘制的目标重定向到打印机
        painter.begin(printer)
        screen = self.editor.grab()
        painter.drawPixmap(10,10,screen)
        painter.end()
        print("print")


if __name__ == ‘__main__‘:
    app = QtWidgets.QApplication(sys.argv)
    gui = PrintSupport()
    gui.show()
    app.exec_()

技术图片

显示打印对话框

‘‘‘

显示打印对话框

‘‘‘
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QTextEdit, QFileDialog, QDialog
from PyQt5.QtPrintSupport import QPageSetupDialog, QPrintDialog, QPrinter
import sys


class PrintDialog(QWidget):
    def __init__(self):
        super(PrintDialog,self).__init__()
        self.printer = QPrinter()
        self.initUI()



    def initUI(self):
        self.setGeometry(300, 300, 500, 400)
        self.setWindowTitle(‘打印对话框‘)

        self.editor = QTextEdit(self)
        self.editor.setGeometry(20,20,300,270)

        self.openButton = QPushButton(‘打开文件‘,self)
        self.openButton.move(350,20)

        self.settingsButton = QPushButton(‘打印设置‘,self)
        self.settingsButton.move(350,50)

        self.printButton = QPushButton(‘打印文档‘,self)
        self.printButton.move(350,80)

        self.openButton.clicked.connect(self.openFile)
        self.settingsButton.clicked.connect(self.showSettingsDialog)
        self.printButton.clicked.connect(self.showPrintDialog)

    # 打开文件
    def openFile(self):
        fname = QFileDialog.getOpenFileName(self,‘打开文本文件‘,‘./‘)
        if fname[0]:
            with open(fname[0],‘r‘,encoding=‘utf-8‘,errors = ‘ignore‘) as f:
                self.editor.setText(f.read())
    # 显示打印设置对话框
    def showSettingsDialog(self):
        printDialog = QPageSetupDialog(self.printer,self)
        printDialog.exec()

    # 显示打印对话框
    def showPrintDialog(self):
        printdialog = QPrintDialog(self.printer,self)
        if QDialog.Accepted == printdialog.exec():
            self.editor.print(self.printer)





if __name__ == ‘__main__‘:
    app = QApplication(sys.argv)
    gui = PrintDialog()
    gui.show()
    sys.exit(app.exec_())

技术图片

打印机

标签:led   none   printer   打开   begin   encoding   mamicode   filename   qt5   

原文地址:https://www.cnblogs.com/wuyuan2011woaini/p/14829940.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!