请帮助我完成此代码。我想做一个文本编辑器,当我在input dialog
中给一个数字时,一些文本或某些符号或一些数字会插入到我的文本行中以在输入对话框中进行编号,并以1开头来输入对话框编号...是代码,您可以知道我想做什么。
请查看代码,并告诉我该怎么做?
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_())
答案 0 :(得分:0)
我不确定我是否理解问题,但以下内容输出您在问题中的要求:
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import (QWidget, QApplication, QTextEdit, QInputDialog, QPushButton, QVBoxLayout, QProgressBar)
import sys
class DollarCalculation(QThread):
reportContent = pyqtSignal(str)
reportProgress = pyqtSignal(int, int)
calculationFinished = pyqtSignal()
def __init__(self, numDollars, currentLines):
super().__init__()
self.numDollars = numDollars
self.currentLines = currentLines
self.symbols = ['$', '@', '#', '!', 1]
def run(self) -> None:
maximum = len(self.symbols)
for i in range(0, len(self.symbols)):
self.addDollarCounter(self.symbols[i])
self.reportProgress.emit(i + 1, maximum)
self.calculationFinished.emit()
def addDollarCounter(self, symbol):
for currentLine in self.currentLines:
for dollar_counter in range(1, self.numDollars + 1):
word = dollar_counter * symbol
if isinstance(word, int):
temp_word = []
while word > 0:
temp_word.insert(0, word)
word = word - 1
word = ''.join(str(e) for e in temp_word)
self.reportContent.emit(currentLine + str(word))
class Tbx(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.dollarCalculation = None
self.te.append('#Hi')
self.te.append('#User')
self.te.append('#agent')
def initUI(self):
self.vbox = QVBoxLayout()
self.btn = QPushButton('ClickMe', self)
self.btn.clicked.connect(self.dollar)
self.te = QTextEdit(self)
self.prgb = QProgressBar(self)
self.vbox.addWidget(self.te)
self.vbox.addWidget(self.btn)
self.vbox.addWidget(self.prgb)
self.setLayout(self.vbox)
self.setGeometry(300, 300, 400, 250)
self.setWindowTitle('Application')
def dollar(self):
text_1_int, ok = QInputDialog.getInt(self, 'HowMany?', 'Enter How Many dollar do you want ?')
if not ok:
return
self.btn.setEnabled(False)
self.prgb.setValue(0)
self.dollarCalculation = DollarCalculation(text_1_int, self.te.toPlainText().split('\n'))
self.dollarCalculation.reportContent.connect(self.appendContent)
self.dollarCalculation.reportProgress.connect(self.progress)
self.dollarCalculation.calculationFinished.connect(self.calculationFinished)
self.dollarCalculation.start()
def appendContent(self, text):
self.te.append(text)
def progress(self, value, maximun):
self.prgb.setMaximum(maximun)
self.prgb.setValue(value)
def calculationFinished(self):
self.btn.setEnabled(True)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Tbx()
ex.show()
sys.exit(app.exec_())