Search code examples
pythonqtpyqt5qlabelqpushbutton

PyQt5 - Why my label is only showing the last line in my file?


I'm new in pyqt5 and I'm working on a gui that runs tasks from a txt file (each line is a task) i have a QLabel(txt_task_user) where i want to display line by line, and the QPushButton(user_f_b) that i want to display the next line whenever i click on it.

here is the file :

task1

task2

task3

task4

task5

task6

task7

task8

task9

task10

I tried this but i don't understand why it only shows the last line (task10) when i click the QPushButton :

def __init__(self) :
    super(Lister, self).__init__()
    loadUi("lister.ui", self)
    self.user_f_b.clicked.connect(self.execute_user_f)

def execute_user_f(self) :
    with open("task.txt", "r") as f :
        lines = f.readlines()
        for line in lines :
            self.txt_task_user.setText(str(line))

i've been searching on the net i found out that i maybe should use signals but i have no idea how to do that i found nothing interesting on the net.


Solution

  • def __init__(self) :
        super(Lister, self).__init__()
        loadUi("lister.ui", self)
        self.user_f_b.clicked.connect(self.execute_user_f)
        self.counter = 0
        self.taskList = []
        with open("task.txt", "r") as f :
            lines = f.readlines()
            for line in lines :
                self.taskList.append(line)
    
    def execute_user_f(self) :
        self.txt_task_user.setText(self.taskList[self.counter])
        self.counter += 1