Python Qt-PyQt5将文本设置为QLineEdit.setText()时,是否可以在每个字母之间设置间隔
我的目标是,当我设置字符串Python Qt-PyQt5将文本设置为QLineEdit.setText()时,是否可以在每个字母之间设置间隔,python,qt,setinterval,pyqt5,Python,Qt,Setinterval,Pyqt5,我的目标是,当我设置字符串QLineEdit.setText(string)时,我希望看到该字符串的字母逐字显示,就像每个字母之间都有间隔一样 我尝试使用pyautogui.typewrite(string,interval=0.25),但没有成功。我还尝试了一个类似于字符串的for循环 tt = "Test" for x in tt: QLineEdit.setText(x) 但它只写了最后一个字符,即t。我想知道有没有可能用PyQt5制作这个 编辑:至少在Qt中可能吗?也许我可以将
QLineEdit.setText(string)
时,我希望看到该字符串的字母逐字显示,就像每个字母之间都有间隔一样
我尝试使用pyautogui.typewrite(string,interval=0.25)
,但没有成功。我还尝试了一个类似于字符串的for循环
tt = "Test"
for x in tt:
QLineEdit.setText(x)
但它只写了最后一个字符,即t。我想知道有没有可能用PyQt5制作这个
编辑:至少在Qt中可能吗?也许我可以将其转换为PyQt?试试:
import time
tt = "Test"
for idx in range(len(tt)+1):
text = tt[:idx]
self.line_edit.setText(text)
QApplication.processEvents()
time.sleep(2)
将2更改为您希望延迟的秒数
<>编辑*解决方案用EcHuMoRO的编辑< /P> < > > C++伪代码和注释(我的Python太生锈,不可用):< /P> 这很简单。调用
setTextDelayed()
时,使用字符串中的第一个字符调用setText()
,并启动计时器。使用position变量跟踪追加的字符数,因此将其设置为1
当计时器启动时,将调用
updateText()
。递增currentTextPos
并使用新的子字符串finalText
调用setText()
。当到达字符串末尾时,停止计时器。我用计时器解决了这个问题,请检查它。不要介意我导入的模块,懒得删除无用的模块
from PyQt5.QtWidgets import (QMessageBox,QApplication, QWidget, QToolTip, QPushButton,
QDesktopWidget, QMainWindow, QAction, qApp, QToolBar, QVBoxLayout,
QComboBox,QLabel,QLineEdit,QGridLayout,QMenuBar,QMenu,QStatusBar,
QTextEdit,QDialog,QFrame,QProgressBar,QHBoxLayout,QGraphicsDropShadowEffect,
QCheckBox
)
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtGui import QIcon,QFont,QPixmap,QPalette,QWindow
from PyQt5.QtCore import QCoreApplication, Qt,QBasicTimer, QTimer,QPoint,QSize
import PyQt5.QtWidgets,PyQt5.QtCore
class Settings(QMainWindow):
def __init__(self):
super().__init__()
self.set_widget = QMainWindow(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
#size
self.setFixedSize(900,423)
#line
self.line_edit = QLineEdit(self)
self.line_edit.setGeometry(100,100,400,35)
self.line_edit.setStyleSheet("color: white;"
"background-color: rgb(0,0,0);"
"border: 1px solid white;"
"border-radius: 10px;"
"font: bold 15pt 'Comic Sans MS'")
self.timer26 = QTimer(self)
self.timer26.timeout.connect(self.timergo)
self.timer26.start(90)
self.cnt = 0
self.lst = ""
def timergo(self):
text = "I'm a text has interval between the letters"
try:
self.lst += text[self.cnt]
self.line_edit.setText("".join(str(self.lst[::])))
self.cnt+=1
except:
print ("index error")
#or just pass
self.show()
app1 = QApplication(sys.argv)
app1.setStyleSheet("QMainWindow{background-color: rgb(0,0,0);border: 2px solid rgb(20,20,20)}")
ex1 = Settings()
sys.exit(app1.exec_())
这应该是相对容易的计时器。缓存最终文本字符串和表示字符串中位置的整数。每次计时器启动时,请添加另一个字符。@JonHarper您能演示一个工作示例吗?您不能在PyQt中使用
time.sleep()
,它有自己的循环,所以程序将冻结。我以前测试过。如果使用time.sleep
windows的主循环不工作,则程序冻结。@GLHF。事实上,这是可行的。您只需将QApplication.processEvents()
放在time.sleep
之前。(显然应该是self.line\u edit.setText(text)
)。@ekhumoro设置好计时器非常优雅。@GLHF。有很多不同的方法来解决这样的问题。使用processEvents
通常可以提供最简单的解决方案。但我主要想指出的是,这个答案并不是完全错误的。
from PyQt5.QtWidgets import (QMessageBox,QApplication, QWidget, QToolTip, QPushButton,
QDesktopWidget, QMainWindow, QAction, qApp, QToolBar, QVBoxLayout,
QComboBox,QLabel,QLineEdit,QGridLayout,QMenuBar,QMenu,QStatusBar,
QTextEdit,QDialog,QFrame,QProgressBar,QHBoxLayout,QGraphicsDropShadowEffect,
QCheckBox
)
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtGui import QIcon,QFont,QPixmap,QPalette,QWindow
from PyQt5.QtCore import QCoreApplication, Qt,QBasicTimer, QTimer,QPoint,QSize
import PyQt5.QtWidgets,PyQt5.QtCore
class Settings(QMainWindow):
def __init__(self):
super().__init__()
self.set_widget = QMainWindow(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
#size
self.setFixedSize(900,423)
#line
self.line_edit = QLineEdit(self)
self.line_edit.setGeometry(100,100,400,35)
self.line_edit.setStyleSheet("color: white;"
"background-color: rgb(0,0,0);"
"border: 1px solid white;"
"border-radius: 10px;"
"font: bold 15pt 'Comic Sans MS'")
self.timer26 = QTimer(self)
self.timer26.timeout.connect(self.timergo)
self.timer26.start(90)
self.cnt = 0
self.lst = ""
def timergo(self):
text = "I'm a text has interval between the letters"
try:
self.lst += text[self.cnt]
self.line_edit.setText("".join(str(self.lst[::])))
self.cnt+=1
except:
print ("index error")
#or just pass
self.show()
app1 = QApplication(sys.argv)
app1.setStyleSheet("QMainWindow{background-color: rgb(0,0,0);border: 2px solid rgb(20,20,20)}")
ex1 = Settings()
sys.exit(app1.exec_())