如何将变量 x(即 html 输入字段的输入字符的长度)访问到 mousePressEvent 函数中?而变量 x 是一个动态变量,随着每个字符的长度而增加。我想控制 mousePressEvent 中的文本输入尺寸。但我无法访问变量 x。
import sys
import os
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets,
QtWebChannel
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.x = 50
self.setGeometry(300, 200, 800, 600)
self.channel = QtWebChannel.QWebChannel(self)
self.channel.registerObject("backend", self)
self.show()
@QtCore.pyqtSlot(str)
def pyFun1(self, x):
self.x = x
print(self.x)
def mousePressEvent(self, e):
self.view = QtWebEngineWidgets.QWebEngineView(self)
self.view.resize(self.x, 40)
self.view.page().setWebChannel(self.channel)
self.view.load(QtCore.QUrl().fromLocalFile(
os.path.split(os.path.abspath(__file__))
[0]+r'\index.html'))
self.view.move(QPoint(e.x(), e.y()))
self.view.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())