C
C
Cockie2022-01-16 04:47:38
Python
Cockie, 2022-01-16 04:47:38

How to change the layout of window widgets correctly?

It is necessary that everything is displayed from left to right by n number, for example, let's say 4 pieces are displayed and then to the next line and again 4

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtCore    import pyqtSignal, QUrl, Qt
from PyQt5.QtGui     import QPixmap
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply
 
class Label(QLabel):
  clicked = pyqtSignal(int)
  def __init__(self, index):
    super().__init__(None)
    self.index = index
  def mousePressEvent(self, event):
    if event.button() == Qt.LeftButton:
      self.clicked.emit(self.index)
 
class Window(QWidget):
  def __init__(self, links, names):
    super().__init__(None)
    self.links = links
    self.names = names
    self.index = -1
    self.setLayout(QVBoxLayout())
    self.manager = QNetworkAccessManager();
    self.manager.finished.connect(self.replyFinished)
    self.download()
  def download(self):
    self.index = self.index + 1
    if self.index < len(self.links):
      url = QUrl(self.links[self.index]);
      request = QNetworkRequest(url)
      self.manager.get(request)
  def replyFinished(self, reply):
    if (reply.error() == QNetworkReply.NoError):
      data = reply.readAll()
      pixmap = QPixmap()
      pixmap.loadFromData(data)
      image = Label(self.index)
      image.setPixmap(pixmap)
      image.clicked.connect(lambda index: print(self.links[index]))
      title = QLabel()
      title.setText(self.names[self.index])
      self.layout().addWidget(image)
      self.layout().addWidget(title)      
      self.download()
 
links = []
links.append("https://upload.wikimedia.org/wikipedia/commons/thumb/3/36/Mars_Valles_Marineris_EDIT.jpg/240px-Mars_Valles_Marineris_EDIT.jpg")
links.append("https://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Saturn_-_April_25_2016_%2837612580000%29.png/320px-Saturn_-_April_25_2016_%2837612580000%29.png")
links.append("https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Jupiter%2C_image_taken_by_NASA%27s_Hubble_Space_Telescope%2C_June_2019_-_Edited.jpg/250px-Jupiter%2C_image_taken_by_NASA%27s_Hubble_Space_Telescope%2C_June_2019_-_Edited.jpg")
 
names =[]
names.append("Изображение Марса на основе 102 снимков, полученных АМС «Викинг-1» 22 февраля 1980 года")
names.append("Фотография Сатурна, снятая космическим аппаратом Кассини. 25.04.2016.")
names.append("Фотография Юпитера, сделанная 27 июня 2019 года с телескопа «Хаббл»")
 
app = QApplication(sys.argv)
window = Window(links, names)
window.show()
sys.exit(app.exec_())

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question