如何在不显示调用带参数的方法下,获取到方法的参数或者方法内的变量 | python | python 技术论坛-大发黄金版app下载

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()

如何在不显示的调用槽函数的情况下取到作用域内的变量或参数。

jason990420
最佳答案

我对 pyqt/pyside 知道的并不多, 以下代码仅供参考 !

from pyside6.qtcore import qt
from pyside6.qtwidgets import (qapplication, qmainwindow, qwidget,
    qvboxlayout, qpushbutton, qlabel)
class window(qmainwindow):
    def __init__(self, *arg, **kwargs):
        super().__init__(*arg, **kwargs)
        self.button = qpushbutton("check size")
        self.button.clicked.connect(self.the_button_was_clicked)
        self.label = qlabel(alignment=qt.alignmentflag.aligncenter)
        self.layout = qvboxlayout()
        self.layout.addwidget(self.button)
        self.layout.addwidget(self.label)
        self.container = qwidget()
        self.container.setlayout(self.layout)
        self.setcentralwidget(self.container)
    def resizeevent(self, event):
        self.width, self.height = event.size().width(), event.size().height()
    def the_button_was_clicked(self):
        self.label.settext(f"size: {self.width} x {self.height}")
app = qapplication([])
window = window()
window.show()
app.exec()

file

1周前
讨论数量: 1
jason990420

我对 pyqt/pyside 知道的并不多, 以下代码仅供参考 !

from pyside6.qtcore import qt
from pyside6.qtwidgets import (qapplication, qmainwindow, qwidget,
    qvboxlayout, qpushbutton, qlabel)
class window(qmainwindow):
    def __init__(self, *arg, **kwargs):
        super().__init__(*arg, **kwargs)
        self.button = qpushbutton("check size")
        self.button.clicked.connect(self.the_button_was_clicked)
        self.label = qlabel(alignment=qt.alignmentflag.aligncenter)
        self.layout = qvboxlayout()
        self.layout.addwidget(self.button)
        self.layout.addwidget(self.label)
        self.container = qwidget()
        self.container.setlayout(self.layout)
        self.setcentralwidget(self.container)
    def resizeevent(self, event):
        self.width, self.height = event.size().width(), event.size().height()
    def the_button_was_clicked(self):
        self.label.settext(f"size: {self.width} x {self.height}")
app = qapplication([])
window = window()
window.show()
app.exec()

file

1周前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
网站地图