python GUI庫圖形界面開發(fā)之PyQt5打印控件QPrinter詳細(xì)使用方法與實(shí)例
PyQt5打印控件QPrinter簡介
打印圖像是圖像處理軟件中的一個(gè)常用功能,打印圖像實(shí)際上是在QPaintDevice中畫圖,與平時(shí)在QWidget。QPixmap和QImage中畫圖是一樣的,都是創(chuàng)建一個(gè)QPainter對象進(jìn)行畫圖的,只是打印使用的是QPrinter,它的本質(zhì)上也是一個(gè)QPainterDevice(繪圖設(shè)備)
QPrinter的使用實(shí)例
import sys from PyQt5.QtWidgets import QApplication,QMainWindow,QLabel,QSizePolicy,QAction from PyQt5.QtPrintSupport import QPrintDialog,QPrinter from PyQt5.QtGui import QImage,QIcon,QPixmap class MainWindow(QMainWindow): def __init__(self,parent=None): super(MainWindow, self).__init__(parent) #設(shè)置標(biāo)題 self.setWindowTitle('打印圖片') #創(chuàng)建標(biāo)簽,設(shè)置標(biāo)簽的大小規(guī)則以及控件的位置居中 self.imageLabel=QLabel() self.imageLabel.setSizePolicy(QSizePolicy.Ignored,QSizePolicy.Ignored) self.setCentralWidget(self.imageLabel) #實(shí)例化Qimage類 self.image = QImage() #自定義的多個(gè)函數(shù),實(shí)現(xiàn)的功能不一 self.createActions() self.createMenus() self.createToolBars() if self.image.load('images\screen.png'): self.imageLabel.setPixmap(QPixmap.fromImage(self.image)) self.resize(self.image.width(),self.image.height()) def createActions(self): #加載圖標(biāo),添加快捷方式,添加提示信息,綁定槽函數(shù) self.PrintAction=QAction(QIcon('images\screen.png'),self.tr('打印'),self) self.PrintAction.setShortcut('Ctrl+P') self.PrintAction.setStatusTip(self.tr('打印')) self.PrintAction.triggered.connect(self.slotPrint) def createMenus(self): #實(shí)例化菜單欄,并添加一個(gè)父菜單,以及把PrintAction添加到父菜單下 PrintMenu=self.menuBar().addMenu(self.tr('打印')) PrintMenu.addAction(self.PrintAction) def createToolBars(self): #在工具欄區(qū)域內(nèi)添加控件printACtion fileToolBar=self.addToolBar('Print') fileToolBar.addAction(self.PrintAction) def slotPrint(self): #實(shí)例化打印圖像對象 printer=QPrinter() #打印窗口彈出 printDialog=QPrintDialog(printer,self) if printDialog.exec_(): painter=QPainter(printer) #實(shí)例化視圖窗口 rect=painter.viewport() #獲取圖片的尺寸 size=self.image.size() size.scale(rect.size(),Qt.KeepAspectRatio) #設(shè)置視圖窗口的屬性 painter.setViewport(rect.x(),rect.y(),size.width(),size.height()) #設(shè)置窗口的大小為圖片的尺寸,并在窗口內(nèi)繪制圖片 painter.setWindow(self.image.rect) painter.drawImage(0,0,self.image) if __name__ == '__main__': app=QApplication(sys.argv) main=MainWindow() main.show() sys.exit(app.exec_())
運(yùn)行程序,顯示效果如下
打印功能
本文詳細(xì)講解了PyQt5打印控件QPrinter詳細(xì)使用方法與實(shí)例,更多關(guān)于PyQt5控件知識請查看下面的相關(guān)鏈接
相關(guān)文章
解決Jupyter無法導(dǎo)入已安裝的 module問題
這篇文章主要介紹了解決Jupyter無法導(dǎo)入已安裝的 module問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04圖文詳解牛頓迭代算法原理及Python實(shí)現(xiàn)
牛頓迭代法又稱為牛頓-拉夫遜(拉弗森)方法,它是牛頓在17世紀(jì)提出的一種在實(shí)數(shù)域和復(fù)數(shù)域上近似求解方程的方法。本文將利用圖文詳解牛頓迭代算法原理及實(shí)現(xiàn),需要的可以參考一下2022-08-08使用Pandas計(jì)算系統(tǒng)客戶名稱的相似度
在日常業(yè)務(wù)處理中,我們經(jīng)常會面臨將不同系統(tǒng)中的數(shù)據(jù)進(jìn)行匹配和比對的情況,本文將介紹如何使用Python的Pandas庫來處理這個(gè)問題,需要的可以參考一下2023-07-07