| 54 | |
| 55 | |
| 56 | class Window(QWidget): |
| 57 | |
| 58 | def __init__(self, *args, **kwargs): |
| 59 | super(Window, self).__init__(*args, **kwargs) |
| 60 | self.resize(600, 400) |
| 61 | layout = QHBoxLayout(self) |
| 62 | |
| 63 | # 左侧 |
| 64 | widgetLeft = QWidget(self) |
| 65 | layoutLeft = QVBoxLayout(widgetLeft) |
| 66 | # 右侧 |
| 67 | self.widgetRight = QListWidget( |
| 68 | self, minimumWidth=200, iconSize=QSize(150, 150)) |
| 69 | self.widgetRight.setViewMode(QListWidget.IconMode) |
| 70 | layout.addWidget(widgetLeft) |
| 71 | layout.addWidget(self.widgetRight) |
| 72 | |
| 73 | self.webView = QWebEngineView() |
| 74 | layoutLeft.addWidget(self.webView) |
| 75 | |
| 76 | # 截图方式一 |
| 77 | groupBox1 = QGroupBox('截图方式一', self) |
| 78 | layout1 = QVBoxLayout(groupBox1) |
| 79 | layout1.addWidget(QPushButton('截图1', self, clicked=self.onScreenShot1)) |
| 80 | layoutLeft.addWidget(groupBox1) |
| 81 | |
| 82 | # 截图方式二(采用js) |
| 83 | groupBox2 = QGroupBox('截图方式二', self) |
| 84 | layout2 = QVBoxLayout(groupBox2) |
| 85 | self.codeEdit = QLineEdit( |
| 86 | 'body', groupBox2, placeholderText='请输入需要截图的元素、ID或者class:如body、#id .class') |
| 87 | layout2.addWidget(self.codeEdit) |
| 88 | self.btnMethod2 = QPushButton( |
| 89 | '', self, clicked=self.onScreenShot2, enabled=False) |
| 90 | layout2.addWidget(self.btnMethod2) |
| 91 | layoutLeft.addWidget(groupBox2) |
| 92 | |
| 93 | # 提供访问接口 |
| 94 | self.channel = QWebChannel(self) |
| 95 | # 把自身对象传递进去 |
| 96 | self.channel.registerObject('_self', self) |
| 97 | # 设置交互接口 |
| 98 | self.webView.page().setWebChannel(self.channel) |
| 99 | # 支持截图 |
| 100 | settings = QWebEngineSettings.globalSettings() |
| 101 | settings.setAttribute(QWebEngineSettings.ScreenCaptureEnabled, True) |
| 102 | self.webView.loadStarted.connect(self.onLoadStarted) |
| 103 | self.webView.loadFinished.connect(self.onLoadFinished) |
| 104 | self.webView.load(QUrl("https://pyqt.site")) |
| 105 | |
| 106 | def onLoadStarted(self): |
| 107 | print('load started') |
| 108 | self.btnMethod2.setEnabled(False) |
| 109 | self.btnMethod2.setText('暂时无法使用(等待页面加载完成)') |
| 110 | |
| 111 | @pyqtSlot(bool) |
| 112 | def onLoadFinished(self, finished): |
| 113 | if not finished: |