pythonguis-examples
pythonguis-examples copied to clipboard
AttributeError: 'QWebEngineView' object has no attribute 'print_'
https://github.com/mfitzp/15-minute-apps/blob/b99eb330310fe7302ead39473ef0d34cccb3d12d/browser/browser.py#L168

OS: win10 x64 Python: 3.5.4 x64 PyQt: 5.10.0
https://github.com/mfitzp/15-minute-apps/blob/b99eb330310fe7302ead39473ef0d34cccb3d12d/browser_tabbed/browser_tabbed.py#L217
Traceback (most recent call last):
File "browser_tabbed.py", line 217, in print_page
dlg.paintRequested.connect(self.browser.print_)
AttributeError: 'MainWindow' object has no attribute 'browser'
I get the same error as above. The error show up in the console log when the user selects the File submenu "File... Print..."
0-MyProjects/PyQt5-15mins-apps/browser_tabbed') Traceback (most recent call last):
File "C:\Users...!0-MyProjects\PyQt5-15mins-apps\browser_tabbed\browser_tabbed-RSL-WIP.py", line 226, in print_page dlg.paintRequested.connect(self.browser.print_)
AttributeError: 'MainWindow' object has no attribute 'browser
Should this dlg.paintRequested.connect(self.browser.print_) refer to QPrintPreviewDialog() or should it refer to print_action instantiated earlier, i.e., print_action.triggered.connect(self.print_page) ?
Why is it using the paintRequested attribute for the .connect() signal, instead of actually invoking the QPrintPreviewDialog box?
Please suggest a fix for this, and clarify the fix with an explanation?
Thank you.
Richard
AttributeError: 'MainWindow' object has no attribute 'browser
@richdevboston You can fix this by adding the method below into the browser_tabbed.MainWindow class.
def handle_paint_request(self, printer):
painter = QPainter(printer)
browser = self.tabs.currentWidget()
painter.setViewport(browser.rect())
painter.setWindow(browser.rect())
browser.render(painter)
painter.end()
And change the print_page method:
def print_page(self):
dlg = QPrintPreviewDialog()
- dlg.paintRequested.connect(self.browser.print_)
+ dlg.paintRequested.connect(self.handle_paint_request)
dlg.exec_()
FYI, these links below can help you if something wrong:
To fix browser.MainWindow.print_page method, the handle_paint_request must be changed a little:
def handle_paint_request(self, printer):
painter = QPainter(printer)
- browser = self.tabs.currentWidget()
- painter.setViewport(browser.rect())
- painter.setWindow(browser.rect())
- browser.render(painter)
+ painter.setViewport(self.browser.rect())
+ painter.setWindow(self.browser.rect())
+ self.browser.render(painter)
painter.end()
HTH Jason