#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_()