0

I have a list of strings that i want to print the next element of the it by pressing a pushbutton each time.

Also, how can i print the previous item of the list by pressing another pushbutton?

Here is a sample code:

from PyQt5 import QtCore, QtWidgets
import sys

class Main(QtWidgets.QMainWindow):

    def __init__(self):
      self.pushbutton = QtWidgets.QPushButton(self)
      self.pushbutton.move(20,20)
      self.list = ["first","second","third"]
      self.setGeometry(300,300,250,180)
      self.pushbutton.clicked.connect(self.showElements)
      self.show()
     
     def showElements(self):
      pass
      
app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Sherafati
  • 196
  • 1
  • 10
  • Please provide some code sample with your question, without any, it is hard for us to help you. From what I understand you could use a variable to keep track of the list index, but we won't be able to write you any code without further details. – vgalin Aug 15 '20 at 14:14

2 Answers2

1

You can use generators to have the functionality you asked

For example:

a = [1,2,3]

def myFunction():
    for item in a:
       yield item

# Get iterator
iterator = myFunction()

# Call this on every button push
nextItem = next(iterator)
print(nextItem)

Here's a working repl.it project:

https://repl.it/@HarunYlmaz/generators-iterators

Harun Yilmaz
  • 8,281
  • 3
  • 24
  • 35
  • Thanks, but how can i iterate backwards in the list? i want to be able to keep track of the elements of the list. – Sherafati Aug 15 '20 at 14:21
  • 1
    Reversing will not be as straightforward as this one. [This thread](https://stackoverflow.com/questions/1561214/python-reverse-generator) may help – Harun Yilmaz Aug 15 '20 at 14:25
  • How about doing something like `newlist = iter(a)` and then calling next on `newlist`? this way there is no need to definea generator function – Sherafati Aug 15 '20 at 14:26
  • 1
    Of course, you're right. You can create a generator for the list using `iter(a)`. – Harun Yilmaz Aug 15 '20 at 14:39
  • 1
    Also, you can create your own iterator as described in [this answer](https://stackoverflow.com/a/57503797/1331040) to have `prev` functionality. – Harun Yilmaz Aug 15 '20 at 14:42
1

Try it:

import sys
from PyQt5 import QtCore, QtWidgets


class Example(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        
        self.centralWidget = QtWidgets.QWidget()
        self.setCentralWidget(self.centralWidget)

        self.list = ["first", "second", "third"]
        self.len_list = len(self.list) - 1
        self.index = 0

        self.label = QtWidgets.QLabel(self.list[self.index])
        
        button1 = QtWidgets.QPushButton('next')
        button1.clicked.connect(self.search_next)
        button2 = QtWidgets.QPushButton('previous')
        button2.clicked.connect(self.search_previous)
        
        layout = QtWidgets.QGridLayout(self.centralWidget)
        layout.addWidget(self.label, 0, 0, 1, 2, alignment=QtCore.Qt.AlignHCenter)
        layout.addWidget(button2, 1, 0)
        layout.addWidget(button1, 1, 1)        
     
    def search_next(self):
        if self.index >= self.len_list:
            self.index = 0
        else:
            self.index += 1
        self.label.setText(self.list[self.index])
        
    def search_previous(self):
        if self.index <= 0:
            self.index = self.len_list
        else:
            self.index -= 1
        self.label.setText(self.list[self.index])        
      
      
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33