Reputation: 57
I made a pyqt5 qlistwidget and I want to select an item from qlistwidget and then pass into another B File, but there is an error and I don't know how to fix it
A .py
import sys
from PyQt5.QtWidgets import (QListWidget, QWidget, QMessageBox,
QApplication, QVBoxLayout)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
vbox = QVBoxLayout(self)
listWidget = QListWidget()
listWidget.addItem("sparrow")
listWidget.addItem("robin")
listWidget.addItem("crow")
listWidget.itemDoubleClicked.connect(self.onClicked)
vbox.addWidget(listWidget)
self.setLayout(vbox)
self.setGeometry(300, 300, 350, 250)
self.setWindowTitle('QListWidget')
self.show()
def onClicked(self, item):
print(item.text())
return item.text()
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
B .py
from A import Example
Example().onClicked()
Appear Error TypeError: onClicked() missing 1 required positional argument: 'item'
Upvotes: 0
Views: 83
Reputation: 140
In your example you are directly calling onClicked that won't work.
TypeError: onClicked() missing 1 required positional argument: 'item'
This error is suggesting that you are not passing item to onClicked ( as you are directly calling it like Example().onClicked()
Solution
You can declare a method handleClick
on File B
and call it from File A
onClicked
File A
from B import handleClick
class Example ...
...
...
onClicked(self,item):
handleClick(item.text()) #calling the method at File B with item.text()
File B
def handleClick(text):
print(text)
Upvotes: 2