简体   繁体   English

PyQt5:如何在函数“ dollar()”中完成此代码

[英]PyQt5 : How to complete this code in the function 'dollar()'

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. 我想做一个文本编辑器,当我在input dialoginput dialog一个数字时,一些文本或某些符号或一些数字会插入到我的文本行中,以在输入对话框中进行编号,并以1开头来输入对话框编号。以下是代码,您可以知道我想做什么。 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_())

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: 如果我在文本输入中输入3:

在此处输入图片说明 在此处输入图片说明

Btw, the dollar_counter increment is useless: it will be handled by the for loop. 顺便说一句, dollar_counter增量是无用的:它将由for循环处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM