访问在另一个 class 的方法中创建的变量
Access variables created in a method of another class
我想调用另一个 class 的变量,而不必将其设为全局变量。我做不到!你能帮帮我吗?
class labTreeMap(QLabel):
clickedQL = pyqtSignal()
def mouseReleaseEvent(self,event):
if event.button() == Qt.LeftButton:
xTM = event.pos().x()
yTM = event.pos().y()
self.clickedQL.emit()
class TreeMap(QWidget):
def click_TreeMap(self,c):
xTM=labTreeMap.mouseReleaseEvent.xTM # recall xTM from here
yTM=labTreeMap.mouseReleaseEvent.yTM # recall yTM from here
使它们成为实例变量。
class labTreeMap(QLabel):
clickedQL = pyqtSignal()
def mouseReleaseEvent(self,event):
if event.button() == Qt.LeftButton:
self.xTM = event.pos().x()
self.yTM = event.pos().y()
self.clickedQL.emit()
class TreeMap(QWidget):
def click_TreeMap(self,c):
xTM=someLabTreeMap.xTM # recall xTM from here
yTM=someLabTreeMap.yTM # recall yTM from here
将 someLabTreeMap
替换为包含先前调用 mouseReleaseEvent()
的 labTreeMap
对象的变量。
理想情况下,您需要使 TreeMap
依赖实例化时提供给它的 labTreeMap
实例。
以下是您的 类 的拙劣副本,但显示了 TreeMap
如何获取和 labTreeMap
的实例:
class labTreeMap:
def foo(self):
self.xTM = 0
class TreeMap:
def __init__(self, ltm):
self.ltm = ltm
def click_TreeMap(self,c):
xTM=self.ltm.xTM # recall xTM from here
l = labTreeMap()
l.foo() # create the xTM attribute
t = TreeMap(l)
t.click_TreeMap(0)
在 PyQt 中执行此操作的惯用方法是将值作为信号参数发出,以便它们可以直接用作任何连接槽中的参数:
class LabTreeMap(QLabel):
clickedQL = pyqtSignal(int, int) # declare paramter types
def mouseReleaseEvent(self,event):
if event.button() == Qt.LeftButton:
xTM = event.pos().x()
yTM = event.pos().y()
self.clickedQL.emit(xTM, yTM) # emit paramters
class TreeMap(QWidget):
def __init__(self):
...
self.treeMapChart = TreeMapChart()
...
self.labTreeMap = LabTreeMap()
self.labTreeMap.clickedQL.connect(self.click_TreeMap)
def click_TreeMap(self, xTM, yTM):
# do something with parameters
print(f'{xTM=}, {yTM=}')
# do something with chart
print(self.treeMapChart)
我想调用另一个 class 的变量,而不必将其设为全局变量。我做不到!你能帮帮我吗?
class labTreeMap(QLabel):
clickedQL = pyqtSignal()
def mouseReleaseEvent(self,event):
if event.button() == Qt.LeftButton:
xTM = event.pos().x()
yTM = event.pos().y()
self.clickedQL.emit()
class TreeMap(QWidget):
def click_TreeMap(self,c):
xTM=labTreeMap.mouseReleaseEvent.xTM # recall xTM from here
yTM=labTreeMap.mouseReleaseEvent.yTM # recall yTM from here
使它们成为实例变量。
class labTreeMap(QLabel):
clickedQL = pyqtSignal()
def mouseReleaseEvent(self,event):
if event.button() == Qt.LeftButton:
self.xTM = event.pos().x()
self.yTM = event.pos().y()
self.clickedQL.emit()
class TreeMap(QWidget):
def click_TreeMap(self,c):
xTM=someLabTreeMap.xTM # recall xTM from here
yTM=someLabTreeMap.yTM # recall yTM from here
将 someLabTreeMap
替换为包含先前调用 mouseReleaseEvent()
的 labTreeMap
对象的变量。
理想情况下,您需要使 TreeMap
依赖实例化时提供给它的 labTreeMap
实例。
以下是您的 类 的拙劣副本,但显示了 TreeMap
如何获取和 labTreeMap
的实例:
class labTreeMap:
def foo(self):
self.xTM = 0
class TreeMap:
def __init__(self, ltm):
self.ltm = ltm
def click_TreeMap(self,c):
xTM=self.ltm.xTM # recall xTM from here
l = labTreeMap()
l.foo() # create the xTM attribute
t = TreeMap(l)
t.click_TreeMap(0)
在 PyQt 中执行此操作的惯用方法是将值作为信号参数发出,以便它们可以直接用作任何连接槽中的参数:
class LabTreeMap(QLabel):
clickedQL = pyqtSignal(int, int) # declare paramter types
def mouseReleaseEvent(self,event):
if event.button() == Qt.LeftButton:
xTM = event.pos().x()
yTM = event.pos().y()
self.clickedQL.emit(xTM, yTM) # emit paramters
class TreeMap(QWidget):
def __init__(self):
...
self.treeMapChart = TreeMapChart()
...
self.labTreeMap = LabTreeMap()
self.labTreeMap.clickedQL.connect(self.click_TreeMap)
def click_TreeMap(self, xTM, yTM):
# do something with parameters
print(f'{xTM=}, {yTM=}')
# do something with chart
print(self.treeMapChart)