如何在不显示调用带参数的方法下,获取到方法的参数或者方法内的变量
class aaaa(object):
def init(self):
super(aaaa, self).init()
def a(self, width, height):
self.width = width
def b(self):
这里如何引用到self.width的值,或者width的值
说明:a是一个槽函数,实时接收主窗口类发送过来的窗口的尺寸变化,所以程序知道参数是多少而人不知道,所以不能使用self.a(width, height)来调用槽函数。
为什么非要调用a作用域的变量或参数,因为类aaaa是一个绘图函数,绘图的逻辑必须在paintEvent(self, event):中进行,这是pyqt的内置函数不可该名称,不可以当作槽函数来使用,试过在槽函数中调用:
self.resize(new_width, new_height) # 更新 绘图窗口 的尺寸
self.update() # 触发重绘
重绘可以生效,窗口尺寸不会更新
试过的方法
1:
def init(self):
super(aaaa, self).init()
self.width = None
def a(self, width, height):
self.width = width
调用构造函数self.width的结果始终为空,a槽函数的打印结果,始终正确
2:
试过闭包,发现怎么也绕不开手动填写width, height参数的问题
class MyClass:
def method_B(self, width, height):
width = “局部变量”
def closure():
w = width
return closure
def method_A(self):
closure = self.method_B(1, 2)
closure()
如何在不显示的调用槽函数的情况下取到作用域内的变量或参数。
我对
PyQt/PySide
知道的并不多, 以下代码仅供参考 !