0

I'm new in pyqt5 and I'm working on a gui that runs tasks from a txt file (each line is a task) and I have 8 QTextBrowser's ,i want to display each line into a QTextBrowser (for example the first line will be shown in the first QTextBrowser, the second line in the second QTextBrowser ..., but the ninth line will be displayed in the first QTextBrowser , the tenth line in the second QTextBrowser ...) and keep looping thru my eight QTextBrowser's

I tried something but it didn't work :

def execute_all(self) :
    file = open("taches.txt")
    lines = file.readlines()
    t1 = QTextBrowser()
    t2 = QTextBrowser()
    t3 = QTextBrowser()
    t4 = QTextBrowser()
    t5 = QTextBrowser()
    t6 = QTextBrowser()
    t7 = QTextBrowser()
    t8 = QTextBrowser()
    for line in lines :
        for i in t1,t2,t3,t4,t5,t6,t7,t8 :
            widget.addWidget(i)
            widget.setCurrentIndex(widget.currentIndex()+1)
            self.i.setText(line)

the gui i'm talking about !

Thanks for your support!

1 Answers1

1

Use itertools.cycle(). You have to first create all the widgets, and then cycle through them while iterating the lines.

You cannot also use setText(), as it overwrites any existing content. Instead, use append().

Also note that QTextBrowser is intended for navigation purposes, if you only need a read only QTextEdit, just call setReadOnly(True) or add the property in the constructor.

def execute_all(self):
    editors = []
    for i in range(8):
        editor = QTextEdit(readOnly=True)
        editors.append(editor)
        widget.addWidget(editor)

    with open("taches.txt") as file:
        lines = file.readlines()

    it = itertools.cycle(editors)
    for line in lines:
        editor = next(it)
        editor.append(line)

Note that you shall not use global references like you did with widget, nor attempt to change the current index like that; if you're following a YouTube tutorial named "Code First from Hala", then be aware that it's known to provide a lot of terrible suggestions and bad practices, and I strongly suggest you to avoid it completely.

musicamante
  • 41,230
  • 6
  • 33
  • 58
  • Hi thanks for your answer, i tried your answer but it writes 4 first lines in a new QTextEdit created in a new form instead of writing in the QTextEdit's already created ?! –  Jun 12 '22 at 15:55
  • @aymane_420 then it means that you created those text edits *previously*. I answered your question based on what you provided, we cannot help you with code we know nothing about. – musicamante Jun 12 '22 at 16:04
  • i'm sorry but i noticed that in the picture below :/ –  Jun 12 '22 at 16:24
  • Images are just references, if you provide code, we answer based on *that* code, and sice in your code you're creating *new* instances, I've done the same in my answer. If those objects already exist, why are you creating new ones? – musicamante Jun 12 '22 at 17:16
  • so how can i loop thru the 8 QTextEdit if they are already created ? –  Jun 12 '22 at 17:23
  • The concept is exactly the same: use `chain` with a list of the existing objects. – musicamante Jun 12 '22 at 17:56
  • 1
    @aymane_420 sorry, this is not a tutorial website. You're clearly doing something wrong and we cannot really help you in every step you're taking. For instance, if you're getting that error, it means that you are using *strings* as items in `editor`. So: please do your own research on the different *object types* and also the difference between and usage of *classes* and *instances*. – musicamante Jun 12 '22 at 23:52