Sorry for the long post but wished to give as much as poss
There is a lot of code not shown here but I'm trying to clean up a huge function of buttons in a PyQt5 Gui
I have a GUI output and all is working well and I'm now making an attempt to remove the repeated code and so creating a function to create the buttons.
In a functioned name initUI I have around 20 buttons. As can be seen from the code, the old way was creating each one separately.
What I have done is to create a function that the parameters are sent to and this then creates them.
This does work apart from defRun arg sent to button. This is passing a call to another function I've highlighted with >>>>arg<<<< this isn't really in the code
class iac2tf(QMainWindow):
def __init__(self):
super(iac2tf, self).__init__()
self.initUI()
self.setGeometry(0, 0, 1700, 1000)........
def button (self, buttonName, buttonText, >>>>defRun<<<<, buttonWidth, buttonHeight, buttonx, buttony):
self.buttonName = QtWidgets.QPushButton(buttonText,self)
>>>>self.buttonName.clicked.connect(lambda:self.defRun)<<<<
self.buttonName.resize(buttonWidth,buttonHeight)
self.buttonName.move(buttonx,buttony)
self.buttonName.show()
def initUI(self):
#passed style
self.openFilebutton = self.button('openFile', 'Open File', >>>'open()'<<<< ,110,30,5,50)
#Old style
self.ProcessFile = QtWidgets.QPushButton('Process File',self)
self.ProcessFile.clicked.connect(self.processFile)
self.ProcessFile.resize(110,30)
self.ProcessFile.move(5, 80)
/......
....../
def open(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
file, _ = QFileDialog.getOpenFileNames(self, 'Ope.......
app = QApplication(sys.argv)
win = iac2tf()
win.show()
I've tried passing Knowing some would fail but wanted to cover all bases and was exasperated
- str(open)
GUI opens with
<built-in function open>
On clicking button
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'
- str(open())
self.openFilebutton = self.button('openFile', 'Open File', str(open()) ,110,30,5,50)
TypeError: Required argument 'file' (pos 1) not found
- str(self.open)
GUI opens with
<bound method iac2tf.open of <__main__.iac2tf object at 0x7f87e73da948>>
On clicking button
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'
- str(self.open())
Open file selection menu ie starts open func
- self.open
GUI starts
<bound method iac2tf.open of <__main__.iac2tf object at 0x7fdcb2dee948>>
On clicking button
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.open'
- 'self.open()'
- 'open()'
- 'open'
opens GUI without error
on clicking button
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'
- open
opens GUI with
<built-in function open>
on clicking button
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'
- open()
self.openFilebutton = self.button('openFile', 'Open File', open() ,110,30,5,50)
TypeError: Required argument 'file' (pos 1) not found
- passing to a local to func var all of above but is as is :(
self.buttonName.clicked.connect(defRun)andself.openFilebutton = self.button('openFile', 'Open File', self.open, 110,30,5,50)