我正在尝试从PyQt5 MDI GUI应用程序将html打印到物理打印机。我的代码崩溃了应用程序并生成堆栈跟踪。我无法弄清楚为什么。
代码成功调用打印对话框。单击对话框中的“打印”后,应用程序立即崩溃。我的电脑从其他应用程序成功打印到所选的打印机。
(html变量包含有效的html。我可以用它在webView上创建一个PDF文件。)
任何建议都将不胜感激!
Ubuntu Linux 16.04.03 PyQt 5.10 Python 3.5
from PyQt5.QtPrintSupport import (
QPrintDialog,
QPrinter
)
from PyQt5.QtWebEngineWidgets import (
QWebEngineView
)
def PrintRoutine(self):
webView = QWebEngineView()
webView.setZoomFactor(1)
def printMe():
def callback(is_ok):
if is_ok:
print('printing finished')
else:
print('printing error')
myPrinter = QPrinter(QPrinter.HighResolution)
printDialog = QPrintDialog(myPrinter, self)
if printDialog.exec_():
webView.page().print(myPrinter, callback)
webView.loadFinished.connect(printMe)
webView.setHtml(html) # html is defined elsewhere in the code
堆栈跟踪是:
Received signal 11 <unknown> 000000000000
#0 0x7f36e12af9a5 <unknown>
#1 0x7f36dffef501 <unknown>
#2 0x7f36e12afd3d <unknown>
#3 0x7f36f2525390 <unknown>
#4 0x7f36df0431bd QPrinter::pageRect()
#5 0x7f36e6779c6d <unknown>
#6 0x7f36e677a1b6 <unknown>
#7 0x7f36e005cc6f <unknown>
#8 0x7f36e1332c19 <unknown>
#9 0x7f36e12cbf48 <unknown>
#10 0x7f36e12cd66f <unknown>
#11 0x7f36e12cdbc0 <unknown>
#12 0x7f36e0010805 <unknown>
#13 0x7f36ee28cadb QObject::event()
#14 0x7f36e6d2854c QApplicationPrivate::notify_helper()
#15 0x7f36e6d2f897 QApplication::notify()
#16 0x7f36e7773c9e sipQApplication::notify()
#17 0x7f36ee2609f8 QCoreApplication::notifyInternal2()
#18 0x7f36ee26348b QCoreApplicationPrivate::sendPostedEvents()
#19 0x7f36ee2b5563 <unknown>
#20 0x7f36eb34d197 g_main_context_dispatch
#21 0x7f36eb34d3f0 <unknown>
#22 0x7f36eb34d49c g_main_context_iteration
#23 0x7f36ee2b4baf QEventDispatcherGlib::processEvents()
#24 0x7f36d7cc69a1 <unknown>
#25 0x7f36ee25f26a QEventLoop::exec()
#26 0x7f36ee267da4 QCoreApplication::exec()
#27 0x7f36e75ff3e0 meth_QApplication_exec_
#28 0x0000004e9b7f PyCFunction_Call
#29 0x0000005372f4 PyEval_EvalFrameEx
#30 0x00000053b7e4 PyEval_EvalFrameEx
#31 0x000000540199 <unknown>
#32 0x000000540e4f PyEval_EvalCode
#33 0x00000054a7c5 <unknown>
#34 0x0000004e9b7f PyCFunction_Call
#35 0x0000005372f4 PyEval_EvalFrameEx
#36 0x00000053b7e4 PyEval_EvalFrameEx
#37 0x00000053b7e4 PyEval_EvalFrameEx
#38 0x00000053b7e4 PyEval_EvalFrameEx
#39 0x000000540199 <unknown>
#40 0x00000053bd92 PyEval_EvalFrameEx
#41 0x00000053b7e4 PyEval_EvalFrameEx
#42 0x00000053b7e4 PyEval_EvalFrameEx
#43 0x000000540199 <unknown>
#44 0x000000540e4f PyEval_EvalCode
#45 0x00000060c272 <unknown>
#46 0x00000060e71a PyRun_FileExFlags
#47 0x00000060ef0c PyRun_SimpleFileExFlags
#48 0x00000063fb26 Py_Main
#49 0x0000004cfeb1 main
#50 0x7f36f216a830 __libc_start_main
#51 0x0000005d6049 _start
r8: 00007f36f250eba8 r9: 0000000000000000 r10: 00007f36f250eb78 r11: 00007f36f250eb78
r12: 000000000362b890 r13: 00007f36e677a190 r14: 00007ffc8b6259a0 r15: 00007ffc8b625920
di: 00007ffc8b625700 si: 00007f36e7a68aa8 bp: 00000000026052e0 bx: 00007ffc8b625700
dx: 0000000000000009 ax: 00007f36e75a6460 cx: 0000000000000000 sp: 00007ffc8b6256f0
ip: 00007f36df0431bd efl: 0000000000010206 cgf: 002b000000000033 erf: 0000000000000000
trp: 000000000000000d msk: 0000000000000000 cr2: 0000000000000000
[end of stack trace]
Calling _exit(1). Core file will not be generated.
答案 0 :(得分:1)
[从编辑到问题中提取的答案]
在PyQt5中将HTML打印到物理打印机的简单方法。
此技术允许用户选择和配置物理打印机,然后创建临时QTextDocument以呈现HTML,最后调用文档对象的本机print_方法进行打印。它很简单,但我无法在网上找到一个简单的例子。
from PyQt5.QtPrintSupport import QPrintDialog, QPrinter
from PyQt5.QtGui import QTextDocument
def PrintRoutine(self):
html = ("
<html>
<body>
Hello world!
</body>
</html>
")
# create a QPrinter object for the printer the user later selects
myPrinter = QPrinter()
# let user select and configure a printer, saved in the object created above
myDialog = QPrintDialog(myPrinter, self)
# execute the print if the user clicked "Print"
if myDialog.exec_():
# create a QTextDocument in memory to hold our HTML
myDocument = QTextDocument()
# load the html into the document
myDocument.setHtml(html)
# send the html to the physical printer
myDocument.print_(myPrinter)