i have a python that includes a list that handle the path of existing PDF FILES in the selected folder.
what i want is to make the system print in the console the selected item when the user click on the item.
so it must be connect to an itemclick event.
i will appreciate any help.
the system display this exception :
this error occure 'NoneType' object has no attribute 'text'
this is how i tried to make it : self.listWidgetPDFlist.itemClicked.... how to continue?
create an empty list
fileList=[]
loop over the selected folder and add the matched item to the widgetList
for root,dirs,files in os.walk(directory):
for filename in files:
if filename.endswith('.pdf'):
t=os.path.join(directory,filename)
print(t)
#fileList.extend(t)
fileList.append(t)
# add the list into the listWidgetPDFlist
self.listWidgetPDFlist.addItems(fileList)
upon selection of an item make the system print in the console the current item
self.listWidgetPDFlist.itemClicked(print(self.listWidgetPDFlist.currentItem().text()))
the problem is in the above line of code
Item clicked is a signal and must be connected to function.
self.listWidgetPDFlist.itemClicked.connect(lambda: print(self.listWidgetPDFlist.currentItem().text()))
The line above uses a lambda statement to print the selected item in a single line of code. This will work for very simple things like you are trying to do here, but you can also connect this to a method to do more.
self.listWidgetPDFlist.itemClicked.connect(self.print_item)
def print_item(self):
print(self.listWidgetPDFlist.currentItem().text())
# handle other items here
When connecting to a method you must leave the parentheses off the methods name when connecting it to the signal.