zoukankan      html  css  js  c++  java
  • PyQt5打印机

    1、打印机操作(打印默认文本里面的内容)
    from PyQt5 import QtGui,QtWidgets,QtPrintSupport
    from PyQt5.QtWidgets import *
    import sys

    class Printsupport1(QMainWindow):
    def __init__(self):
    super(Printsupport1,self).__init__()
    self.setGeometry(500,200,300,300)
    self.button=QPushButton("打印QtextEdit控件中的内容",self)
    self.button.setGeometry(20,60,260,200)
    self.editor=QTextEdit("默认文本",self)
    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=QApplication(sys.argv)
    p=Printsupport1()
    p.show()
    sys.exit(app.exec_())

    2、显示打印样式设置对话框
    from PyQt5.QtPrintSupport import QPageSetupDialog,QPrintDialog,QPrinter
    from PyQt5.QtWidgets import *
    import sys

    class Printdialog(QMainWindow):
    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.settingbutton=QPushButton("打印设置",self)
    self.settingbutton.move(350,50)

    self.printbutton=QPushButton("打印文档",self)
    self.printbutton.move(350,80)

    self.openbutton.clicked.connect(self.openfile)
    self.settingbutton.clicked.connect(self.showsettingdailog)
    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 showsettingdailog(self):
    printerdailog=QPageSetupDialog(self.printer,self)
    printerdailog.exec()

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

    if __name__=="__main__":
    app=QApplication(sys.argv)
    p=Printdialog()
    p.show()
    sys.exit(app.exec_())

  • 相关阅读:
    [转]Release版程序调试排错技巧
    关于获得MFC窗口其它类指针的方法(CSDN)
    MFC .DLL指南(二)
    对“仅通过崩溃地址找出源代码的出错行”一文的补充与改进,转自vckbase,记录一下
    [转]调试Release版本应用程序,不知道转自哪里
    MFC的DLL 概述
    从今天开始每天写C++或其他学习的知识的笔记,以激励自己
    近日发现vs2005安装的一个问题
    VC中处理C1010错误的两种方法
    [转]VC调试篇不知道转自何处
  • 原文地址:https://www.cnblogs.com/Yanjy-OnlyOne/p/12297861.html
Copyright © 2011-2022 走看看