PyQt5打印机

lianback 2020-02-12

1、打印机操作(打印默认文本里面的内容)from PyQt5 import QtGui,QtWidgets,QtPrintSupportfrom PyQt5.QtWidgets import *import sysclass 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_())

PyQt5打印机

2、显示打印样式设置对话框from PyQt5.QtPrintSupport import QPageSetupDialog,QPrintDialog,QPrinterfrom PyQt5.QtWidgets import *import sysclass 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_())

PyQt5打印机

相关推荐

王磊的程序员之路 / 0评论 2020-06-26
lianback / 0评论 2020-05-19