1
  1. What can I write here to when I pressed the button, delete all blank lines in QTextEdit
  2. How can I do this:

text in TextEditor:

Hello
Goodbye
Jack
Max

now when I pressed the button insert 12 after or before:

Hello12
Goodbye12
Jack12
Max12

Code:

from PyQt5.QtWidgets import (QPushButton,QApplication,QWidget,QTextEdit,QVBoxLayout)
import sys

class MyApp(QWidget):
    def __init__ (self):
        super().__init__()
        self.initUI()
    def initUI(self):
        self.btn = QPushButton('MyFirstButton',self)
        self.vbox = QVBoxLayout(self)
        self.txt_ed = QTextEdit(self)
        self.vbox.addWidget(self.txt_ed)
        self.vbox.addWidget(self.btn)

        self.btn.clicked.connect(self.btn_func)

        self.setGeometry(300,300,300,450)
        self.setWindowTitle('MyTextApp')
        self.show()

    def btn_func(self):
        pass
        #? 1-What can I write here to when I pressed the button , delete all blank lines in QTextEdit
        #? 2. How can I do this:

        #text in TextEditor:
        #Hello
        #Goodbye
        #Jack
        #Max
        #-----------------------------
        #now when I pressed the button:
        #Hello12
        #Goodbye12
        #Jack12
        #Max12
        #-----------------------------
        #insert 12 after or before.
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Jack Parten
  • 59
  • 2
  • 11

1 Answers1

1
  1. Delete all blank lines
import re

# ...
class MyApp(QWidget):
    # ...
    def btn_func(self):
        original = self.txt_ed.toPlainText().split('\n')
        # https://stackoverflow.com/a/3711884
        filtered = filter(lambda x: not re.match(r'^\s*$', x), original)
        self.txt_ed.setPlainText("\n".join(filtered))

  1. Insert "12" at the end of each line
def btn_func(self):
    lines = self.txt_ed.toPlainText().split('\n')
    word = "12"
    new_lines = [text + word for text in lines]
    self.txt_ed.setPlainText("\n".join(new_lines))
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • No insert somethings I mean when I pressed the button insert to all lines like it : the text : jack hello goodbye ------------ insert '.org' to all lines like jack.org hello.org – Jack Parten Apr 29 '19 at 12:20
  • 1
    @JackParten I do not understand you, explain yourself better. In your question 1 you say eliminate the blank lines, and in question 2 you say add something text to the QTextEdit. Is my answer wrong or is your question misspelled? – eyllanesc Apr 29 '19 at 12:22
  • insert before or after the all line of text – Jack Parten Apr 29 '19 at 12:24
  • @JackParten Placing that type of information in the comments is not good because the format of the comments generates confusion, if you want help edit and improve your question clearly indicating what you want, I recommend reading [ask] and pass the [tour] if not you've done – eyllanesc Apr 29 '19 at 12:26
  • Thanks for delete blank lines it worked. but I can't insert something to all lines see the btn_func in code I edited it see it to know what I say – Jack Parten Apr 29 '19 at 12:46
  • @JackParten I recommend you write your question clearly, this way you save us time and effort. I recommend asking a friend or partner if the question you have proposed is clear or not. Or at least take the time to meditate if the way you post your question is the best you can do. – eyllanesc Apr 29 '19 at 12:53
  • ok man thank you . do you have any messangers like TelegramMessenger in your phone? – Jack Parten Apr 29 '19 at 12:55
  • I want be friend with you and ask some question if I need in somewhere . because in stackoverflow I can just ask 1 question at the day – Jack Parten Apr 29 '19 at 12:59
  • you know . I want learn but in my country is not more way to learn and my enlish is weak and bad to learn from English courses – Jack Parten Apr 29 '19 at 13:01
  • @JackParten I only answer the questions I want in SO. If you want to have the ability to ask more questions then you gain reputation: https://stackoverflow.com/help/whats-reputation , bye – eyllanesc Apr 29 '19 at 13:02
  • 1
    @JackParten That's enough. Requesting for contact information from other users is not appropriate. If you have a new question, you will have to wait until the site allows you to ask a new one. – E_net4 Apr 29 '19 at 13:06