2016-06-17 321 views
0

我的目标是,当我设置字符串QLineEdit.setText(string)时,我希望看到该字符串的字母以字母出现,就像那里是每个字母之间的间隔。Qt-PyQt5是否可以在设置文本时将每个字母之间的间隔设置为QLineEdit.setText()

我试图使用pyautogui.typewrite(string,interval = 0.25)但它没有工作。我也尝试了一个循环的字符串像;

tt = "Test" 
for x in tt: 
    QLineEdit.setText(x) 

但它只写了最后一个字符是t。我想知道是否可以在PyQt5中做到这一点?

编辑: Qt中至少有可能吗?也许我可以将其转换为PyQt?

+0

这应该是相对容易的一个计时器。缓存最终的文本字符串和表示字符串中位置的整数。每当定时器启动时,添加另一个字符。 –

+0

@JonHarper你能举个实例吗? – GLHF

回答

-2

尝试:

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至然而,许多秒您想要的延迟。

与ekhumoro的编辑

+0

你不能在PyQt中使用'time.sleep()',它有自己的循环,所以程序会冻结。我之前测试过它。如果使用'time.sleep',则窗口的主循环不起作用,因此程序冻结。 – GLHF

+1

@GLHF。其实,这会起作用。你只需要在'time.sleep'之前加上'QApplication.processEvents()'。 (显然它应该是'self.line_edit.setText(text)')。 – ekhumoro

+0

@ekhumoro设置一个定时器是非常优雅。 – GLHF

0

在C++伪代码和注释更新编辑*解决方案(我的Python是太生疏是有用的):

class DelayedLineEdit : public QLineEdit 
{ 
    ... 
public Q_SLOTS: 
    void setTextDelayed(const QString &txt); 
protected Q_SLOTS: 
    void updateText(); 
private: 
    QTimer textTimer; 
    QString finalText; 
    int currentTextPos; 
} 

void DelayedLineEdit::setTextDelayed(const QString &txt) 
{ 
    finalText = txt; 
    currentTextPos = 1; 
    setText(finalText.left(currentTextPos); 
    //connect the timer to updateText() 
    //start your timer 
} 

void DelayedLineEdit::updateText() 
{ 
    //increment currentTextPos and check if it's equal to the length of finalText 
    //call setText with finalText.left(currentTextPos) 
    //stop the timer if needed 
} 

这是非常简单的。当调用setTextDelayed()时,可以用字符串中的第一个字符调用setText()并启动计时器。使用位置变量跟踪您追加的字符数,因此将其设置为1

定时器启动时,调用updateText()。增加currentTextPos,并调用setText(),新的子字符串为finalText。当到达字符串末尾时,停止计时器。

0

我用一个计时器解决了这个问题,把它检查出来。不介意我导入的模块,也懒得删除无用的模块。

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_()) 
相关问题