Python 3.x 将python函数作为参数传递给PyQt5中的函数
很抱歉发了这么长的邮件,但我希望尽可能多地提供 这里没有显示很多代码,但我正在尝试清理PyQt5 Gui中的大量按钮功能 我有一个GUI输出,一切正常,我现在尝试删除重复的代码,并创建一个函数来创建按钮 在一个函数名initUI中,我有大约20个按钮。从代码中可以看出,旧方法是分别创建每一个 我所做的是创建一个函数,将参数发送到该函数,然后创建它们 除了发送到按钮的defRun arg之外,这也可以工作。Python 3.x 将python函数作为参数传递给PyQt5中的函数,python-3.x,arguments,pyqt5,Python 3.x,Arguments,Pyqt5,很抱歉发了这么长的邮件,但我希望尽可能多地提供 这里没有显示很多代码,但我正在尝试清理PyQt5 Gui中的大量按钮功能 我有一个GUI输出,一切正常,我现在尝试删除重复的代码,并创建一个函数来创建按钮 在一个函数名initUI中,我有大约20个按钮。从代码中可以看出,旧方法是分别创建每一个 我所做的是创建一个函数,将参数发送到该函数,然后创建它们 除了发送到按钮的defRun arg之外,这也可以工作。 这是将调用传递给另一个函数,我用>>>>arg突出显示了这是一个将函数作为参数传递的示例
这是将调用传递给另一个函数,我用>>>>arg突出显示了这是一个将函数作为参数传递的示例
class Template(QWidget):
def __init__(self):
super().__init__()
self.vbox = QVBoxLayout(self)
self.create_button('Open', self.open)
self.create_button('Close', self.close)
def create_button(self, text, slot):
btn = QPushButton(text)
btn.clicked.connect(slot)
self.vbox.addWidget(btn)
def open(self):
QFileDialog.getOpenFileName(self)
还值得注意的是,arg buttonName从未实际用于此函数:
def button (self, buttonName, buttonText, >>>>defRun<<<<, buttonWidth, buttonHeight, buttonx, buttony):
self.buttonName = QtWidgets.QPushButton(buttonText,self)
. . .
该变量只存在于函数的局部范围内,self.buttonName完全引用另一个对象。也就是说,如果您想引用您创建的一个按钮,其中传递了buttonName参数的“openFile”,那么它不会被称为self.openFile,而是字面上的self.buttonName。但是,您可以使用exec来实现这一点,尽管通常不鼓励这样做 提供一个Ok谢谢,明天就可以了:self.buttonName.clicked.connectdefRun和self.openFilebutton=self。按钮“openFile”,“openFile”,“self.Open”,110,30,5,50干杯就可以了。你是在想lambda会被绊倒吗?你能解释一下吗?我正在学习我所说的是一种假设,因为你提供的代码不能保证它正常工作,所以我希望你提供我要求你提供的信息,让你提供更多细节。干杯,我来试试。这可能是个大问题。我假设这个按钮将被命名为OpenFile只是查看我的错误,它实际上说的是buttonName。。。。现在觉得很傻吗
<built-in function open>
File "/home/bob/present/proj/WorksArea/gui.py", line 38, in <lambda>
self.buttonName.clicked.connect(lambda:self.defRun)
AttributeError: 'iac2tf' object has no attribute 'defRun'
self.openFilebutton = self.button('openFile', 'Open File', str(open()) ,110,30,5,50)
TypeError: Required argument 'file' (pos 1) not found
<bound method iac2tf.open of <__main__.iac2tf object at 0x7f87e73da948>>
Traceback (most recent call last):
File "/home/bob/present/proj/WorksArea/gui.py", line 38, in <lambda>
self.buttonName.clicked.connect(lambda:self.defRun)
AttributeError: 'iac2tf' object has no attribute 'defRun'
<bound method iac2tf.open of <__main__.iac2tf object at 0x7fdcb2dee948>>
Traceback (most recent call last):
File "/home/bob/present/proj/WorksArea/gui.py", line 38, in <lambda>
self.buttonName.clicked.connect(lambda:self.defRun)
AttributeError: 'iac2tf' object has no attribute 'defRun'
Traceback (most recent call last):
File "/home/bob/present/proj/WorksArea/gui.py", line 38, in <lambda>
self.buttonName.clicked.connect(lambda:self.defRun)
AttributeError: 'iac2tf' object has no attribute 'defRun'
<built-in function open>
Traceback (most recent call last):
File "/home/bob/present/proj/WorksArea/gui.py", line 38, in <lambda>
self.buttonName.clicked.connect(lambda:self.defRun)
AttributeError: 'iac2tf' object has no attribute 'defRun'
self.openFilebutton = self.button('openFile', 'Open File', open() ,110,30,5,50)
TypeError: Required argument 'file' (pos 1) not found
class Template(QWidget):
def __init__(self):
super().__init__()
self.vbox = QVBoxLayout(self)
self.create_button('Open', self.open)
self.create_button('Close', self.close)
def create_button(self, text, slot):
btn = QPushButton(text)
btn.clicked.connect(slot)
self.vbox.addWidget(btn)
def open(self):
QFileDialog.getOpenFileName(self)
def button (self, buttonName, buttonText, >>>>defRun<<<<, buttonWidth, buttonHeight, buttonx, buttony):
self.buttonName = QtWidgets.QPushButton(buttonText,self)
. . .