[英]How to get the text of the clicked item of Listwidget in PyQt5?
我正在尋找一種方法來獲取 listWidget 上單擊的元素的文本/名稱。
這是我的方法看起來像:
# call lamp clicked event
self.listWidget_lamps.itemClicked.connect(self.lamp_clicked)
# call group clicked event
def lamp_clicked(self):
self.lamp_on = True
self.group_on = False
lamp = Lamp(self.item.text())
print("lamp" + self.item.text() + "got clicked")
但它總是崩潰並給我這個錯誤:
lamp = Lamp(self.item.text())
AttributeError: 'MainWindow' object has no attribute 'item'
有人可以告訴我我做錯了什么嗎?
您的 slot lamp_clicked
的簽名是錯誤的。 查看QListWidget::itemClicked並注意信號有一個參數,但您的插槽沒有參數。
def lamp_clicked(self, clickedItem):
self.lamp_on = True
self.group_on = False
lamp = Lamp(clickedItem.text())
print("lamp" + clickedItem.text() + "got clicked")
應該做的伎倆。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.