gpt4 book ai didi

python - 从pyqt5的主页打印

转载 作者:行者123 更新时间:2023-12-01 03:16:07 24 4
gpt4 key购买 nike

我在 pyqt5 中编写了一些代码,在 main main 中创建一个表:

class Ui_MainWindow(object):
def setupUi(self, MainWindow): ...
def retranslateUi(self, MainWindow):...
self.pushButton.setText(_translate("MainWindow", "print"))
self.pushButton.clicked.connect(self.printer)
def printer(self):...

并通过以下方式使用此类:

app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

我想知道如何从程序的主页进行打印?

最佳答案

使用 QPrinter 可以相当容易地完成此操作类。

下面是如何执行此操作的详细注释示例。

import sys

from PyQt5 import QtGui, QtWidgets, QtPrintSupport


class App(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
# Create some widgets
self.setGeometry(500, 500, 300, 300)
self.button = QtWidgets.QPushButton(
'Print QTextEdit widget (the one below)', self)
self.button.setGeometry(20, 20, 260, 30)
self.editor = QtWidgets.QTextEdit(
'Wow such text why not change me?', self)
self.editor.setGeometry(20, 60, 260, 200)
self.button.clicked.connect(self.print_widget)

def print_widget(self):
# Create printer
printer = QtPrintSupport.QPrinter()
# Create painter
painter = QtGui.QPainter()
# Start painter
painter.begin(printer)
# Grab a widget you want to print
screen = self.editor.grab()
# Draw grabbed pixmap
painter.drawPixmap(10, 10, screen)
# End painting
painter.end()

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

要打印整个窗口,只需将 screen = self.editor.grab() 替换为 screen = self.grab()

关于python - 从pyqt5的主页打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42455904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com