from PyQt5.QtWidgets import QApplication, QListWidget, QListWidgetItem app = QApplication([]) list_widget = QListWidget() for i in range(10): item = QListWidgetItem("Item {}".format(i)) list_widget.addItem(item) list_widget.show() app.exec_()
from PyQt5.QtWidgets import QApplication, QListWidget, QListWidgetItem app = QApplication([]) def on_item_selection_changed(): selected_items = list_widget.selectedItems() for item in selected_items: print("Selected item:", item.text()) list_widget = QListWidget() list_widget.itemSelectionChanged.connect(on_item_selection_changed) for i in range(10): item = QListWidgetItem("Item {}".format(i)) list_widget.addItem(item) list_widget.show() app.exec_()In this example, we create a `on_item_selection_changed` slot function that prints the text labels of the currently selected items to the console. We connect the `itemSelectionChanged` signal to this function using the `connect` method. When the user selects an item in the list, the `on_item_selection_changed` function will be called and the selected items will be printed to the console. The PyQt5 package library is used in these examples.