PyQt5 : How to complete this code in the function

2019-08-23 12:49发布

问题:

Please help me to complete this code. I want make a text editor and when I give a number in the input dialog, some text or some symbol or some numbers insert to my text lines to number in input dialog and starts with 1 to input dialog number ... Below is the code, you can know what I want to do. Please see the code and tell me how can I do this?

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

class Tbx(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.vbox = QVBoxLayout()
        self.btn = QPushButton('ClickMe',self)
        self.btn.clicked.connect(self.dollar)
        self.te = QTextEdit(self)
        self.vbox.addWidget(self.te)
        self.vbox.addWidget(self.btn)
        self.setLayout(self.vbox)
        self.setGeometry(300,300,400,250)
        self.setWindowTitle('Application')
        self.show()
def dollar(self):
    text_1_int , ok = QInputDialog.getInt(self,'HowMany?','Enter How Many dollar do you want ?')
    if not ok:
        return
    try:
        current_lines = self.te.toPlainText().split('\n')
        new_lines = list()
        for dollar_counter in range(1, text_1_int + 1):
            word = '$' * dollar_counter
            new_lines += [text + word for text in current_lines]
        self.te.setPlainText("\n".join(new_lines))
                    #I want this:
                    #...Texts in TextEditor at first:
                    #Hi
                    #User
                    #agent
                    #========================================================================
                    #...Text in TextEditor when I press the button and give 3 in InputDialog:
                    #Hi$
                    #Hi$$
                    #Hi$$$
                    #User$
                    #User$$
                    #User$$$
                    #agent$
                    #agent$$
                    #agent$$$
                    #Hi@
                    #Hi@@
                    #Hi@@@
                    #User@
                    #User@@
                    #User@@@
                    #agent@
                    #agent@@
                    #agent@@@
                    #Hi#
                    #Hi##
                    #Hi###
                    #User#
                    #User##
                    #User###
                    #agent#
                    #agent##
                    #agent###
                    #Hi!
                    #Hi!!
                    #Hi!!!
                    #User!
                    #User!!
                    #User!!!
                    #agent!
                    #agent!!
                    #agent!!!
                    #Hi1
                    #Hi12
                    #Hi123
                    #User1
                    #User12
                    #User123
                    #agent1
                    #agent12
                    #agent123
                    #========================================================================
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Tbx()
    sys.exit(app.exec_())

回答1:

You are replacing the the text in your text edit at each iteration. The easiest (clearer) way to do that, would be to generate all your lines before trying to add it to the text edit.

For example:

def dollar(self):
    text_1_int , ok = QInputDialog.getInt(self,'HowMany?','Enter How Many dollar do you want ?')
    if not ok:
        return
    try:
        current_lines = self.te.toPlainText().split('\n')
        new_lines = list()
        for dollar_counter in range(1, text_1_int + 1):
            word = '$' * dollar_counter
            new_lines += [text + word for text in current_lines]
        self.te.setPlainText("\n".join(new_lines))
    except:
        error_msg = QMessageBox()
        error_msg.setIcon(QMessageBox.Critical)
        error_msg.setText('Please Enter Just Number')
        error_msg.setWindowTitle("Error")
        error_msg.exec_()

If I enter 3 in the text input:

Btw, the dollar_counter increment is useless: it will be handled by the for loop.