makeItemв классе
ListWidgetкод :
import sys
import requests
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.Qt import *
class Widget(QWidget):
def __init__(self, urls):
super().__init__()
self.grid = QGridLayout(self)
for i, url in enumerate(urls):
lbl = QLabel()
image = QImage()
image.loadFromData(requests.get(url).content)
if i == 0:
lbl.setPixmap(QPixmap(image).scaled(210, 210))
self.grid.addWidget(lbl, i+1, 0, 4, 1)
else:
lbl.setPixmap(QPixmap(image).scaled(70, 70))
self.grid.addWidget(lbl, i, 1, 1, 1)
class ListWidget(QListWidget):
def __init__(self, *args, **kwargs):
super(ListWidget, self).__init__(*args, **kwargs)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setEditTriggers(self.NoEditTriggers)
self.setFlow(self.LeftToRight)
self.setWrapping(True)
self.setResizeMode(self.Adjust)
self.setSpacing(10)
# Резиновая лента (для эффекта выбора рамки)
self._rubberPos = None
self._rubberBand = QRubberBand(QRubberBand.Rectangle, self)
def makeItem(self, lb):
item = QtWidgets.QListWidgetItem(self)
item.setSizeHint(QtCore.QSize(300, 300))
self.setItemWidget(item, lb)
class MyWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('ListWidget')
self.resize(1100, 670)
self.show()
self.listWidget = ListWidget()
box = QtWidgets.QVBoxLayout(self)
box.addWidget(self.listWidget)
self.urls = [
"https://img.icons8.com/ios/452/phone.png",
"https://img.icons8.com/ios/452/phone.png",
"http://www.google.com/favicon.ico",
"https://www.imgonline.com.ua/examples/random-pixels-wallpaper-big.jpg",
"https://laguna-akul.ru/upload/003/u379/f4/61/8f68d93a.jpg",
]
num = 5
for i in range(num):
art = Widget(self.urls)
self.listWidget.makeItem(art)
QApplication.processEvents()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = MyWindow()
sys.exit(app.exec_())
import sys
# import requests
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.Qt import *
# Картинка для заполнения
IMAGE = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x04\x00\x00\x00\xb5\xfa7\xea\x00\x00\x00\x02sBIT\x08\x08U\xecF\x04\x00\x00\x00\tpHYs\x00\x00\x00v\x00\x00\x00v\x01N{&\x08\x00\x00\x00\x19tEXtSoftware\x00www.inkscape.org\x9b\xee<\x1a\x00\x00\x01\x03IDAT(S}\xd1\xb1jS\x01\x14\xc6\xf1\x1fvi\xda\xc5*\x18\x93!\t\x82\x98-\x84\xd6\xe2\xe0+\xd8\xa1\x04\xbd\x08\xc1\'s0f\x11|\x02!q\xb8\xd0\xa1\xc1L\x9dj\xbaT\xe2\x94w\xf8:\xdc\xf4\xb6\x0e\x96\xb3}\xfc\xf9\xce\x9fs\xc4\xdd(\xac\xfdq\xf2O\x16\xa1\xe1\xbd\x83p\xa1\xe9\x85\xb3p\xe0\x83\xbd-`O\xe9\xbb\x95\x85Yx\xe4\xa7s\xbf}UjT\xc0[_B\xdfq];\xf02L\xbc\xa9\x80\xa6+\x9d\x08mcc\xed\x08=+\xcfn\x1d~9\x0c\xa76\xa6\xa66N\xc3\xd0"\x82w\xce\x94\xa1e\xb3\xed\xe9\xd8h\x87\xb9\xa5\x11\xd7^\xd9\t\x85i\xed05\x8e\xd0\xb5\xe6\xaff<\x04|t\xe1\xc7\x03+",\xbc\xfe\xafdxl\xa5\x1f\xa1\xa5PhE\xe8\xb9\xf4\xa4\x02\x86\xbe\x85\x8eA\xed0\xd4\x0b\x13G\x15\xb0\xab\xf4\xd9\x95\xa5Y\x84\xb9\x85K\x93\xfa\xd4a\xdf\'\xcf\xc3RW\xd72<U\xd4\xcf\xba\x1b#kk\xa3\xfb\xd9\ru"\xad\xb2\x11\xa2\xcfy\x00\x00\x00\x00IEND\xaeB`\x82'
class Widget(QWidget):
def __init__(self, urls):
super().__init__()
self.grid = QGridLayout(self)
for i, url in enumerate(urls):
lbl = QLabel()
image = QImage()
image.loadFromData(IMAGE)
if i == 0:
lbl.setPixmap(QPixmap(image).scaled(210, 210))
self.grid.addWidget(lbl, i + 1, 0, 4, 1)
else:
lbl.setPixmap(QPixmap(image).scaled(70, 70))
self.grid.addWidget(lbl, i, 1, 1, 1)
class ListWidget(QListWidget):
def __init__(self, *args, **kwargs):
super(ListWidget, self).__init__(*args, **kwargs)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setEditTriggers(self.NoEditTriggers)
self.setFlow(self.LeftToRight)
self.setWrapping(True)
self.setResizeMode(self.Adjust)
self.setSpacing(10)
# Резиновая лента (для эффекта выбора рамки)
self._rubberPos = None
self._rubberBand = QRubberBand(QRubberBand.Rectangle, self)
def makeItem(self, lb):
item = QtWidgets.QListWidgetItem(self)
item.setSizeHint(QtCore.QSize(300, 300))
self.setItemWidget(item, lb)
class MyWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('ListWidget')
self.resize(1100, 670)
self.show()
self.listWidget = ListWidget()
# кнопка
self.push_button = QPushButton('del 0 item')
box = QtWidgets.QVBoxLayout(self)
# добавляем кнопку
box.addWidget(self.push_button)
box.addWidget(self.listWidget)
self.urls = [
"https://img.icons8.com/ios/452/phone.png",
"https://img.icons8.com/ios/452/phone.png",
"http://www.google.com/favicon.ico",
"https://www.imgonline.com.ua/examples/random-pixels-wallpaper-big.jpg",
"https://laguna-akul.ru/upload/003/u379/f4/61/8f68d93a.jpg",
]
num = 5
for i in range(num):
art = Widget(self.urls)
self.listWidget.makeItem(art)
# связываем сигнал нажатия на кнопку с методом
self.push_button.clicked.connect(self._on_push_button_clicked)
# метод-обработчик кнопки
def _on_push_button_clicked(self):
# takeItem удаляет итем с виджетом из списка и возвращает его
removed = self.listWidget.takeItem(0)
print(removed)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = MyWindow()
sys.exit(app.exec_())