Прежде чем запустить мой пример, убедитесь, что вы установили PyQt5
pip install PyQt5
pip install pyqt5-tools
PyQt — реализация фреймворка Qt для языка Python.
import sys
from PyQt5.Qt import *
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
list_image = [
['image11.png', 'image21.png', 'image31.png'],
['image12.png', 'image22.png', 'image32.png'],
['image13.png', 'image23.png', 'image33.png'],
]
button = QPushButton("Click my", self)
button.setCheckable(True)
button.clicked.connect(self.button_clicked)
self.layout = QGridLayout(self.centralWidget)
for row in range(len(list_image)):
for column in range(len(list_image[0])):
label = QLabel()
label.setPixmap(QPixmap(list_image[row][column]))
self.layout.addWidget(label, row+1, column+1)
self.layout.setRowStretch(0, 1)
self.layout.setRowStretch(4, 1)
self.layout.setColumnStretch(0, 1)
self.layout.setColumnStretch(4, 1)
self.layout.addWidget(button, 6, 2)
def button_clicked(self, state):
if state:
self.layout.setSpacing(0)
else:
self.layout.setSpacing(10)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Update:
Вадим вам надо помочь из фрагментов составить многомерный список?
нет, именно цельный файл из вот этих фрагментов
import sys
from PyQt5.Qt import *
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
list_image = [
['image11.png', 'image21.png', 'image31.png'],
['image12.png', 'image22.png', 'image32.png'],
['image13.png', 'image23.png', 'image33.png'],
]
button = QPushButton("Click my", self)
button.setCheckable(True)
button.clicked.connect(self.button_clicked)
self.widget = QWidget(self.centralWidget)
self.layout = QGridLayout(self.widget)
for row in range(len(list_image)):
for column in range(len(list_image[0])):
label = QLabel()
label.setPixmap(QPixmap(list_image[row][column]))
self.layout.addWidget(label, row+1, column+1)
self.layout.setRowStretch(0, 1)
self.layout.setRowStretch(4, 1)
self.layout.setColumnStretch(0, 1)
self.layout.setColumnStretch(4, 1)
self.layout_main = QGridLayout(self.centralWidget)
self.layout_main.addWidget(self.widget)
self.layout_main.addWidget(button)
def button_clicked(self, state):
if state:
self.layout.setSpacing(0)
QTimer.singleShot(10, self.save_image)
else:
self.layout.setSpacing(10)
def save_image(self):
fname = "q1334997.png"
pixmap = QPixmap(self.widget.size())
self.widget.render(QPainter(pixmap))
pixmap.save(fname)
self.label_image = QLabel()
self.label_image.setWindowTitle(f'{fname} !!!')
self.label_image.setPixmap(QPixmap(fname))
self.label_image.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
image11.png image21.png image31.png
image12.png image22.png image32.png
image13.png image23.png image33.png