forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 6
/
CallEachWithJs.py
61 lines (48 loc) · 1.81 KB
/
CallEachWithJs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2021/12/15
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: [email protected]
@file: CallEachWithJs.py
@description: 与JS之间的互相调用
"""
import os
from PyQt5.QtCore import QUrl, pyqtSlot
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtWidgets import (QApplication, QLineEdit, QPushButton, QVBoxLayout,
QWidget)
from Lib.WebChannelObject import WebChannelObject
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.m_obj = WebChannelObject(self)
# 注册该窗口,可以访问该窗口的属性,槽函数,信号
# https://doc.qt.io/qt-5/qwidget.html#properties
# https://doc.qt.io/qt-5/qwidget.html#signals
# https://doc.qt.io/qt-5/qwidget.html#public-slots
self.m_obj.registerObject('qtwindow', self)
self.m_obj.start()
layout = QVBoxLayout(self)
self.editTitle = QLineEdit(self, placeholderText='输入标题')
layout.addWidget(self.editTitle)
layout.addWidget(QPushButton('修改标题', self, clicked=self.onChangeTitle))
QDesktopServices.openUrl(
QUrl.fromLocalFile(
os.path.join(os.path.dirname(sys.argv[0] or __file__),
'Data/CallEachWithJs.html')))
def onChangeTitle(self):
self.setWindowTitle(self.editTitle.text())
# ------- 把非槽函数通过pyqtSlot重新暴露 -------
@pyqtSlot(int, int)
def resize(self, width, height):
super().resize(width, height)
if __name__ == '__main__':
import cgitb
import sys
cgitb.enable(format='text')
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())