Results 1 to 4 of 4

Thread: [PyQt5] Making a text appear char by char in QTextEdit

  1. #1

    Default [PyQt5] Making a text appear char by char in QTextEdit

    I want to make an interactive tutorial for special textdocuments. I would like to be able to insert text by the program and also have the user to add something.

    I would like to have the text appear char by char. I tried this, but it just blocks the window from appearing completely, until the text is written out

    import sys
    import time
    from PyQt5 import QtWidgets as qtw

    Qt Code:
    1. class Text_Script(qtw.QTextEdit):
    2. def __init__(self):
    3. super().__init__()
    4.  
    5. def write_intro(self):
    6. welcome_string = "Welcome!"
    7. for i in range(0, len(welcome_string)):
    8. time.sleep(0.2)
    9. self.insertPlainText(welcome_string[i])
    To copy to clipboard, switch view to plain text mode 
    calling in the main window
    Qt Code:
    1. import sys
    2. from Text_Script import Text_Script
    3. from PyQt5 import QtWidgets as qtw
    4.  
    5.  
    6. class Lastenheft(qtw.QWidget):
    7. def __init__(self):
    8. super().__init__()
    9. self.init_me()
    10.  
    11. def init_me(self):
    12. self.setLayout(qtw.QVBoxLayout())
    13. self.text_script = Text_Script()
    14. self.next_btn = qtw.QPushButton("Next")
    15. self.layout().addWidget(self.text_script)
    16. self.layout().addWidget(self.next_btn)
    17. self.showMaximized()
    18. self.text_script.write_intro()
    19.  
    20. if __name__ == '__main__':
    21. app = qtw.QApplication(sys.argv)
    22. mw = Lastenheft()
    23. sys.exit(app.exec())
    To copy to clipboard, switch view to plain text mode 

    Is there a way to achieve this?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,481
    Thanks
    252
    Thanked 711 Times in 701 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: [PyQt5] Making a text appear char by char in QTextEdit

    Your call to sleep() is basically locking the UI until it returns, and your for() loop has no place for the UI event loop to run to update the text document display on screen. So nothing happens until the for() loop exits.

    Instead of using sleep(), use a QTimer instead. Unlike sleep(), QTimer allows the event loop to run while it is counting down, so the UI gets updated.

    What you will have to do is create a slot in your Text_Script class and connect the slot to the QTimer's timeout() signal. In that slot, you will put the next character in the string into the text document, then restart the timer if you haven't reached the end of the string yet.

    You will have to restructure your code to make the string a member variable of the class (so you can access it from the slot) and add another member variable to keep an index to the current character in the string. Each time your slot code runs, you output the character at the index, then increment the index. If the index is less than the length of the string (minus one), you start the timer again, otherwise you do nothing.

    I don't know much python, but I think your code would probably look something like this:

    Qt Code:
    1. class Text_Script(qtw.QTextEdit):
    2. def __init__(self):
    3. super().__init__()
    4. self.timer = QTimer()
    5. self.timer.connect( timer.timeout, whatever the syntax is..., self.putNextCharacterSlot )
    6. self.stringIndex = 0;
    7. self.stringToOutput = QString()
    8.  
    9. def write_intro(self):
    10. self.stringToOutput = "Welcome!"
    11. self.stringIndex = 0;
    12. timer.start( 0.2 )
    13.  
    14. def putNextCharacterSlot( self ) :
    15. if ( self.stringIndex < self.stringToOutput.length() ) :
    16. self.insertPlainText( self.stringToOutput[ self.stringIndex ] )
    17. self.stringIndex++
    18. if ( self.stringIndex < self.stringToOutput.length() - 1 ) :
    19. timer.start( 0.2 )
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3

    Default Re: [PyQt5] Making a text appear char by char in QTextEdit

    Thanks man, works like a charm.

    I had to make some modifications regarding Python3 syntax.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,481
    Thanks
    252
    Thanked 711 Times in 701 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: [PyQt5] Making a text appear char by char in QTextEdit

    I had to make some modifications regarding Python3 syntax.
    Not surprised. I can read python, but I have never written anything serious in it. Thirty years of C++ habits die hard.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 20th June 2018, 19:06
  2. Char Followed by a Char is illegal (qglobal.h)
    By saad_saadi in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2014, 13:19
  3. How to convert unsigned char[] to char *?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 29th April 2011, 08:58
  4. fetching each char entered in Qtextedit??
    By Shuchi Agrawal in forum Newbie
    Replies: 3
    Last Post: 17th February 2007, 11:07
  5. Current char in QTextEdit
    By jlbrd in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2006, 09:30

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.