Python PyQt5 类内函数调用变量方法
在 PyQt5 中,可以使用 'self' 关键字来调用同一个类的值。
例如,如果有一个名为 'MyClass' 的类,其中有一个名为 'my_value' 的变量和一个名为 'my_function' 的函数,可以像下面这样调用:
class MyClass:
def __init__(self):
self.my_value = 10
def my_function(self):
print('My value is:', self.my_value)
# 创建一个 MyClass 实例
my_instance = MyClass()
# 调用 my_function 函数
my_instance.my_function()
在上面的例子中,使用 'self' 关键字来访问同一个类的 'my_value' 变量。在类的构造函数 'init' 中,通过 'self' 来设置 'my_value' 的值。在 'my_function' 中,同样使用 'self' 来访问同一个类的 'my_value' 变量。在创建 'MyClass' 实例 'my_instance' 后,使用 'my_instance' 来调用 'my_function' 函数。
原文地址: https://www.cveoy.top/t/topic/oKpt 著作权归作者所有。请勿转载和采集!