PyQt5:如何在函数“dollar()"中完成这段代码 [英] PyQt5 : How to complete this code in the function 'dollar()'

查看:42
本文介绍了PyQt5:如何在函数“dollar()"中完成这段代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我完成这段代码.我想做一个文本编辑器,当我在 input dialog 中给出一个数字时,一些文本或一些符号或一些数字插入到我的文本行中以在输入对话框中编号,并从 1 开始输入对话框编号... 下面是代码,你可以知道我想做什么.请查看代码并告诉我该怎么做?

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_())

推荐答案

您将在每次迭代时替换文本编辑中的文本.最简单(更清晰)的方法是在尝试将其添加到文本编辑之前生成所有行.

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.

例如:

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_()

如果我在文本输入中输入 3:

If I enter 3 in the text input:

顺便说一句,dollar_counter 增量是无用的:它将由 for 循环处理.

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

这篇关于PyQt5:如何在函数“dollar()"中完成这段代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆