#coding:utf-8
from sys import argv
from PyQt4.QtCore import QFile
from PyQt4.QtCore import QFileInfo
from PyQt4.QtCore import QTextStream
from PyQt4.QtCore import QIODevice
from PyQt4.QtCore import QString
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QMainWindow
from PyQt4.QtGui import QTextEdit
from PyQt4.QtGui import QApplication
from PyQt4.QtGui import QAction
from PyQt4.QtGui import QIcon
from PyQt4.QtGui import QFrame
from PyQt4.QtGui import QColor
from PyQt4.QtGui import QTextCursor
from PyQt4.QtGui import QFileDialog
from PyQt4.QtGui import QMessageBox
class Typing(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)#parent绝对不能省
self.string = ""
self.setWindowTitle("Typing")
self.setGeometry(300, 300, 600, 600)
# self.setWindowFlags(Qt.FramelessWindowHint)
self.open = QAction(QIcon(r"./images/fileopen.png"), "openfile", self)
self.open.setShortcut("Ctrl+O")
# self.connect(self.open,SIGNAL("triggered()"),self,self.fileOpen)
self.open.triggered.connect(self.fileOpen)
self.quit = QAction(QIcon(r"./images/filequit.png"), "quit", self)
self.quit.setShortcut("Ctro+Q")
self.quit.triggered.connect(self.close)
# self.toobar = self.addToolBar("quit")
self.toolbar = self.addToolBar("open")
self.toolbar.setFloatable(False)
self.toolbar.setMovable(False)
self.toolbar.addAction(self.open)
self.toolbar.addAction(self.quit)
self.filename = None
# self.setIconSize(QSize(64,64))
self.editor = MyQTextEdit(self)
self.editor.setTextColor(QColor(229, 229, 229))
self.editor.setFrameShape(QFrame.NoFrame)
self.setCentralWidget(self.editor)
self.getReady()
def getReady(self):
self.editor.setFontFamily("Monaco")
# self.editor.setFontPointSize(9)
self.editor.setTextColor(QColor(230, 219, 116))
self.editor.setStyleSheet(
"background-color: rgb(39, 40, 34);alternate-background-color: rgb(255, 255, 255);font: 9pt 'Monaco';")
# self.string = u"QTextCursor看了大家水电费水电费水电费速度"
self.editor.setText(self.string)
document = self.editor.document()
self.editor.cursor = QTextCursor(document)
self.editor.setTextCursor(self.editor.cursor) #这一句是关键
self.editor.cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor)
# self.cursor.setPosition(self.cursor.position())
# self.cursor.movePosition(QTextCursor.NextCharacter,QTextCursor.KeepAnchor)
# 这一句式关键,要set一下TextCursor才能显示选区
self.editor.setTextCursor(self.editor.cursor)
self.editor.setTextCursor(self.editor.cursor)
def fileOpen(self):
dir = "."
fname = unicode(QFileDialog.getOpenFileName(self,
"Python Editor - Choose File", dir,
"Python files (*.py *.pyw)"))
if fname:
self.filename = fname
self.loadFile()
def loadFile(self):
fh = None
try:
fh = QFile(self.filename)
if not fh.open(QIODevice.ReadOnly):
raise IOError, unicode(fh.errorString())
stream = QTextStream(fh)
stream.setCodec("UTF-8")
self.string = stream.readAll()
self.editor.setTextColor(QColor(229, 229, 229))
self.setWindowTitle("TypingIsFun - %s" % \
QFileInfo(self.filename).fileName())
self.getReady()
except (IOError, OSError), e:
QMessageBox.warning(self, "TypingIsFun -- Load Error",
"Failed to load %s: %s" % (self.filename, e))
finally:
if fh is not None:
fh.close()
class MyQTextEdit(QTextEdit):
def __init__(self, parent=None):
QTextEdit.__init__(self, parent)
document = self.document()#必要
self.cursor = QTextCursor(document)
self.setTextCursor(self.cursor)
def nextChar(self):
self.cursor.setPosition(self.cursor.position())
self.cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor)
self.setTextCursor(self.cursor)
print self.cursor.position()
def nextRow(self):
self.cursor.movePosition(QTextCursor.NextRow)
def previousChar(self):
self.cursor.setPosition(self.cursor.position() - 2)
self.cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.keepAnchor)
self.setTextCursor(self.cursor)
print self.cursor.position()
def mouseDoubleClickEvent(self, event):
pass
def mouseMoveEvent(self, event):
pass
def mousePressEvent(self, event):
pass
def mouseReleaseEvent(self, event):
pass
def keyPressEvent(self, event):
self.key = QString()
if event.key() == Qt.Key_Home:
self.key = "Home"
elif event.key() == Qt.Key_End:
self.key = "End"
elif event.key() == Qt.Key_PageUp:
if event.modifiers() & Qt.ControlModifier:
self.key = "Ctrl+PageUp"
else:
self.key = "PageUp"
elif event.key() == Qt.Key_PageDown:
if event.modifiers() & Qt.ControlModifier:
self.key = "Ctrl+PageDown"
else:
self.key = "PageDown"
elif Qt.Key_A <= event.key() <= Qt.Key_Z:
if event.modifiers() & Qt.ShiftModifier:
self.key = "Shift+"
self.key = event.text()
self.key = QString(self.key)
# if self.key == Qt.Key_Backspace:
# self.privioisChar()
print type(QString("\n"))
print self.key
# print self.cursor.selectedText()
# print unicode(self.cursor.selectedText().decode('utf-8')
if self.key == self.cursor.selectedText():
# QTextCursor.keyPressEvent(self,event)
self.setTextColor(QColor(145, 226, 44))
self.nextChar()
if self.key == Qt.Key_Enter or self.cursor.selectedText() == u'\u2029':
self.nextChar()
# if self.cursor.selectedText() == "\t":
# self.nextChar()
# self.nextChar()
# self.nextChar()
# self.nextChar()
if __name__ == "__main__":
app = QApplication(argv)
tp = Typing()
tp.show()
app.exec_()
在IT行业中,提升打字速度和准确性是一项基本技能,尤其对于程序员、文本编辑者和数据输入员来说至关重要。"码字练习器"是一款专为此目的设计的软件工具,旨在帮助用户提高他们的打字效率。这款练习器可能包含了各种功能,如实时速度显示、错误反馈、多种练习模式等,以满足不同用户的学习需求。 "Typing.py"作为压缩包中的文件名,很可能是指该练习器的源代码文件,使用Python编程语言编写。Python是一种广泛使用的高级编程语言,以其简洁明了的语法和强大的库支持而闻名,因此非常适合开发这样的实用工具。 在"Typing.py"中,我们可以预期找到以下几个核心模块和功能: 1. **用户界面(UI)**:练习器通常会有一个直观的用户界面,展示练习文本、当前打字速度、准确率等信息。Python可以利用Tkinter或PyQt库来创建图形用户界面。 2. **文本生成**:为了练习,程序需要提供不同的文本来源,可能是随机的句子、段落或者文章。这可以通过读取预设的文本文件,或者使用在线API获取实时内容实现。 3. **打字监听与反馈**:程序需要监听用户的输入,并实时评估打字速度和错误率。Python的`keyboard`库可以用来监听用户的键入事件,而`time`库可以帮助计算每秒输入的字符数。 4. **统计与记录**:练习器应该保存用户的练习历史,以便用户跟踪自己的进步。这可能涉及到数据结构如字典或列表来存储每次练习的数据,并可能使用文件操作来持久化这些信息。 5. **多种练习模式**:为了适应不同的学习阶段,码字练习器可能包含多种模式,比如盲打练习、单词打字比赛、文章复制等。每种模式的实现将涉及不同的逻辑处理。 6. **设置与个性化**:用户可能希望自定义练习参数,如字体大小、颜色主题,甚至选择练习的语言。Python的配置管理库如`configparser`可以帮助处理这些设置。 7. **错误提示和纠正**:当用户打错字时,练习器应能立即指出错误并提供纠正建议。这可能通过比较用户的输入与正确文本来实现。 通过"码字练习器",用户可以在日常使用中不断锻炼和提升打字技巧,从而提高工作效率。而"Typing.py"作为其核心代码,是实现这些功能的关键所在。理解并分析这个Python文件,有助于我们深入学习Python编程,特别是GUI编程和事件驱动编程,同时也能让我们了解如何设计和实现一个实用的打字练习应用。



























- 1

- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整

- 粉丝: 7
- 资源: 17
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- wiseflow-社交媒体
- 智慧园区管理系统-活动资源
- OpenManus-manus邀请码
- lilishop 商城 java商城-C语言资源
- 数据结构(C++模板实现)-C++资源
- Sa-Token-Java资源
- JFinal-Python资源
- GiteeIOS-Swift资源
- MATLAB Special Heatmap-Matlab资源
- kotlin-Kotlin资源
- Rust-Rust资源
- Java SE 23 API 源代码
- WeiXinMPSDK-硬件开发资源
- 圣钰SAAS后台管理系统-当前系统只维护不升级.yubb-saas-pro商业版开发中-移动应用开发资源
- skywu520codelib-单片机开发资源
- PandaX-Typescript资源


