0

I have a list:

files = ["image1.png", "image2.png", "image3.png", "image4.png", "image5.png", "image6.png"]

And PyQt5 buttons and a label:

enter image description here

How can I have the first item of the list on the label and go through the list one by one with the:

Next button (click, image2.png, click, image3.png, click, image4.png...)

Previous button (click, image3.png, click, image2.png, click, image1.png...)

And the label should update accordingly.

I've tried all day and searched for answers but couldn't get it to work. This could help maybe, my skills weren't enough to utilize the advice, though.

All the code to create the above window with buttons:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

class App(QWidget):

    def __init__(self):
        super().__init__()
        self._files = deque()
        self._filesCount = len(self._files)
        self._setupUI()
        self._connectSignalsSlots()

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 button - pythonspot.com'
        self.left = 10
        self.top = 10
        self.width = 320
        self.height = 200
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        button = QPushButton('Print Files List', self)
        button.move(100,70)
        button.clicked.connect(self.loadFiles)

        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        button = QPushButton('Print Next Filename', self)
        button.move(160,110)
        button.clicked.connect(self.nextFilename)
        label = QLabel('image1.png (should be first of the list, \n and update with button presses)', self)
        label.move(35,140)

        button = QPushButton('Print Previous Filename', self)
        button.move(10,110)
        button.clicked.connect(self.previousFilename)

        self.show()

    @pyqtSlot()
    def loadFiles(self):
        files = ["image1.png", "image2.png", "image3.png", "image4.png", "image5.png", "image6.png"]
        if len(files) > 0:
            for file in files:
                print(file)

    def nextFilename(self):
        print('nextFilenameButton click')

    def previousFilename(self):
        print('previousFilenameButton click')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
Manu Järvinen
  • 271
  • 2
  • 9

1 Answers1

2

The solution is to handle an index (or cursor) that serves to obtain the filename. And that index must be modified through the buttons

import sys

from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QGridLayout, QLabel, QPushButton, QWidget


class Widget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self._current_index = 0
        self._filenames = []

        self.previous_button = QPushButton("Previous")
        self.next_button = QPushButton("Next")
        self.print_button = QPushButton("Print")
        self.label = QLabel()

        lay = QGridLayout(self)
        lay.addWidget(self.previous_button, 0, 0)
        lay.addWidget(self.next_button, 0, 1)
        lay.addWidget(self.print_button, 0, 2)
        lay.addWidget(self.label, 1, 0, 1, 3)

        self.previous_button.clicked.connect(self.handle_previous)
        self.next_button.clicked.connect(self.handle_next)
        self.print_button.clicked.connect(self.handle_print)

        self._update_button_status(False, True)

        self.load_files()

    def load_files(self):
        self._filenames = [
            "image1.png",
            "image2.png",
            "image3.png",
            "image4.png",
            "image5.png",
            "image6.png",
        ]
        self.current_index = 0

    def handle_next(self):
        self.current_index += 1

    def handle_previous(self):
        self.current_index -= 1

    def handle_print(self):
        for filename in self._filenames:
            print(filename)

    @property
    def current_index(self):
        return self._current_index

    @current_index.setter
    def current_index(self, index):
        if index <= 0:
            self._update_button_status(False, True)
        elif index >= (len(self._filenames) - 1):
            self._update_button_status(True, False)
        else:
            self._update_button_status(True, True)

        if 0 <= index < len(self._filenames):
            self._current_index = index
            filename = self._filenames[self._current_index]
            pixmap = QPixmap(filename)
            self.label.setPixmap(pixmap)

    def _update_button_status(self, previous_enable, next_enable):
        self.previous_button.setEnabled(previous_enable)
        self.next_button.setEnabled(next_enable)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = Widget()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

Note: Having 2 methods with the same name is useless since in python only the last implementation will be used.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Whoops, the same name thing slipped in when I was condensing the code. But thank you so much! This is better than I could've wished for! :) However, what do I need to type to get the current filename for a label text? – Manu Järvinen Jul 15 '21 at 23:48
  • Ah, managed to realize it! Make a new label and add `self.label2.setText(filename)` below the `self.label.setPixmap(pixmap)` line :) – Manu Järvinen Jul 16 '21 at 00:28
  • @ManuJärvinen My example is trivial and I don't intend to implement a lot of functionality. Please avoid adding comments that do not argue about my answer. – eyllanesc Jul 16 '21 at 00:47
  • Sorry :( Was just thinking of people who want to go the next step with this. Thank you for the answer. (Okay, no more comments) – Manu Järvinen Jul 16 '21 at 00:49
  • 1
    @ManuJärvinen Here we do not help to implement projects (since it seems that your comments are going to do it) but to solve a specific problem, we do not intend to cover much but the minimum and necessary because that way we can have a quality Q&A collection. Please read [ask] and review the [tour] – eyllanesc Jul 16 '21 at 00:51