parent
0c8ae29904
commit
a6fe0984ef
@ -1,48 +0,0 @@
|
|||||||
import sys
|
|
||||||
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTextEdit
|
|
||||||
from PyQt5.QtCore import QTimer
|
|
||||||
|
|
||||||
class TypewriterEffectApp(QWidget):
|
|
||||||
def __init__(self, filepath):
|
|
||||||
super().__init__()
|
|
||||||
self.filepath = filepath
|
|
||||||
self.initUI()
|
|
||||||
self.startTyping()
|
|
||||||
|
|
||||||
def initUI(self):
|
|
||||||
# 设置窗口布局和控件
|
|
||||||
layout = QVBoxLayout(self)
|
|
||||||
self.textEdit = QTextEdit(self)
|
|
||||||
self.textEdit.setReadOnly(True)
|
|
||||||
layout.addWidget(self.textEdit)
|
|
||||||
|
|
||||||
# 设置窗口
|
|
||||||
self.setWindowTitle("Typewriter Effect")
|
|
||||||
self.setGeometry(300, 300, 600, 400)
|
|
||||||
|
|
||||||
def startTyping(self):
|
|
||||||
# 读取文件内容
|
|
||||||
with open(self.filepath, 'r') as file:
|
|
||||||
self.content = file.read()
|
|
||||||
|
|
||||||
# 设置定时器逐字显示文本
|
|
||||||
self.index = 0
|
|
||||||
self.timer = QTimer(self)
|
|
||||||
self.timer.timeout.connect(self.displayNextCharacter)
|
|
||||||
self.timer.start(100) # 设置间隔(毫秒)
|
|
||||||
|
|
||||||
def displayNextCharacter(self):
|
|
||||||
if self.index < len(self.content):
|
|
||||||
self.textEdit.insertPlainText(self.content[self.index])
|
|
||||||
self.index += 1
|
|
||||||
else:
|
|
||||||
self.timer.stop()
|
|
||||||
|
|
||||||
def main():
|
|
||||||
app = QApplication(sys.argv)
|
|
||||||
ex = TypewriterEffectApp('path_to_text_file.txt') # 替换为您的文件路径
|
|
||||||
ex.show()
|
|
||||||
sys.exit(app.exec_())
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
Loading…
Reference in new issue