| 36 | class Window(QWidget): |
| 37 | |
| 38 | def __init__(self, *args, **kwargs): |
| 39 | super(Window, self).__init__(*args, **kwargs) |
| 40 | self.resize(600, 400) |
| 41 | layout = QHBoxLayout(self) |
| 42 | |
| 43 | # 左侧 |
| 44 | widgetLeft = QWidget(self) |
| 45 | layoutLeft = QVBoxLayout(widgetLeft) |
| 46 | # 右侧 |
| 47 | self.widgetRight = QListWidget( |
| 48 | self, minimumWidth=200, iconSize=QSize(150, 150)) |
| 49 | self.widgetRight.setViewMode(QListWidget.IconMode) |
| 50 | layout.addWidget(widgetLeft) |
| 51 | layout.addWidget(self.widgetRight) |
| 52 | |
| 53 | self.webView = QWebView() |
| 54 | layoutLeft.addWidget(self.webView) |
| 55 | |
| 56 | # 截图方式一 |
| 57 | groupBox1 = QGroupBox('截图方式一', self) |
| 58 | layout1 = QVBoxLayout(groupBox1) |
| 59 | layout1.addWidget(QPushButton('截图1', self, clicked=self.onScreenShot1)) |
| 60 | layoutLeft.addWidget(groupBox1) |
| 61 | |
| 62 | # 截图方式二(采用js) |
| 63 | groupBox2 = QGroupBox('截图方式二', self) |
| 64 | layout2 = QVBoxLayout(groupBox2) |
| 65 | self.codeEdit = QLineEdit( |
| 66 | 'body', groupBox2, placeholderText='请输入需要截图的元素、ID或者class:如body、#id .class') |
| 67 | layout2.addWidget(self.codeEdit) |
| 68 | self.btnMethod2 = QPushButton( |
| 69 | '', self, clicked=self.onScreenShot2, enabled=False) |
| 70 | layout2.addWidget(self.btnMethod2) |
| 71 | layoutLeft.addWidget(groupBox2) |
| 72 | |
| 73 | # 开启开发人员工具 |
| 74 | QWebSettings.globalSettings().setAttribute( |
| 75 | QWebSettings.DeveloperExtrasEnabled, True) |
| 76 | self.webView.loadStarted.connect(self.onLoadStarted) |
| 77 | self.webView.loadFinished.connect(self.onLoadFinished) |
| 78 | self.webView.load(QUrl("https://pyqt.site")) |
| 79 | |
| 80 | # 暴露接口和加载完成后执行jquery等一些库文件 |
| 81 | self.webView.page().mainFrame().javaScriptWindowObjectCleared.connect( |
| 82 | self.populateJavaScriptWindowObject) |
| 83 | |
| 84 | def populateJavaScriptWindowObject(self): |
| 85 | self.webView.page().mainFrame().addToJavaScriptWindowObject( |