Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from PyQt5 import QtCore, QtGui, QtWidgets
- from PyQt5.QtGui import QColor
- from PyQt5.QtCore import Qt, QEvent, QObject
- import random
- class UserInputClass(QtWidgets.QLineEdit):
- def __init__(self):
- QtWidgets.QLineEdit.__init__(self)
- def keyPressEvent(self, keyEvent):
- super(UserInputClass, self).keyPressEvent(keyEvent)
- if keyEvent.key() == Qt.Key_Backspace:
- print('Backspace pressed')
- class Ui_MainWindow(QtWidgets.QMainWindow):
- def __init__(self):
- super().__init__()
- self.initUI()
- def initUI(self):
- self.setWindowTitle("TypeJam")
- self.centralwidget = QtWidgets.QWidget(self)
- self.setCentralWidget(self.centralwidget)
- self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
- self.setGeometry(300, 300, 800, 500)
- self.initUserInput()
- self.initTextForTyping()
- self.initButtons()
- self.addLayouts()
- self.inputProcessing()
- def initTextForTyping(self):
- self.textForTyping = QtWidgets.QTextEdit(self)
- self.textForTyping.setGeometry(30, 30, 580, 280)
- fontTextForTyping = QtGui.QFont()
- fontTextForTyping.setFamily("Arial")
- fontTextForTyping.setPointSize(20)
- self.textForTyping.setFont(fontTextForTyping)
- self.textForTyping.setReadOnly(True)
- self.textForTyping.setText(self.GetTextFromFile())
- self.str_textForTyping = self.textForTyping.toPlainText()
- def initUserInput(self):
- self.userInput = UserInputClass()
- self.userInput.setGeometry(30, 320, 580, 40)
- fontUserInput = QtGui.QFont()
- fontUserInput.setFamily("Arial")
- fontUserInput.setPointSize(20)
- self.userInput.setFont(fontUserInput)
- self.str_userInput = str()
- self.userInput.textChanged.connect(self.inputProcessing)
- # self.userInput.returnPressed.connect(self.backspace)
- # self.kpa = self.KeyPressEater()
- # self.userInput.installEventFilter(self.kpa)
- def initButtons(self):
- self.buttonStart = QtWidgets.QPushButton("Start", self)
- self.buttonStop = QtWidgets.QPushButton("Stop", self)
- def addLayouts(self):
- self.gridLayout.addWidget(self.textForTyping, 0, 0, 1, 1)
- self.gridLayout.addWidget(self.userInput, 1, 0, 1, 1)
- self.verticalLayout = QtWidgets.QVBoxLayout()
- self.verticalLayout.addWidget(self.buttonStart)
- self.verticalLayout.addWidget(self.buttonStop)
- self.spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
- self.verticalLayout.addItem(self.spacerItem)
- self.gridLayout.addLayout(self.verticalLayout, 0, 1)
- def GetTextFromFile(self):
- textfile = open("text.txt", "r")
- self.TextFromFile = textfile.read()
- textfile.close()
- return self.TextFromFile
- # class KeyPressEater(QObject):
- # def eventFilter(self, obj, event):
- # if event.type() == QEvent.KeyPress:
- # print("Ate key press", event.key())
- # return True
- # else:
- # # standard event processing
- # return QObject.eventFilter(self, obj, event)
- # def eventFilter(self, obj, event):
- # if obj == self.userInput:
- # if event.type() == QEvent.KeyPress:
- # print ("Ate key press", event.key())
- # return True
- # else:
- # return False
- # else:
- # return QtWidgets.QMainWindow.eventFilter(self, obj, event)
- # def keyPressEvent(self, event):
- # if event.key() == Qt.Key_Backspace:
- # print("back")
- # def backspace(self):
- # print("backback")
- # def keyPressEvent(self, QKeyEvent):
- # if QKeyEvent.key() == Qt.Key_Backspace:
- # print("backspace")
- def inputProcessing(self):
- current_userInput = self.userInput.text()
- if len((self.userInput.text())) != 0:
- lastSymbol = self.userInput.text()[len(self.userInput.text()) - 1]
- numberOfRight = 1
- if self.str_textForTyping.find(self.str_userInput + lastSymbol) == 0:
- self.str_userInput += lastSymbol
- numberOfRight += 1
- else:
- # self.textForTyping.setStyleSheet("background: rgb(120,120,120)")
- print(self.str_userInput)
- if " " in self.userInput.text():
- self.userInput.clear()
- if __name__ == "__main__":
- app = QtWidgets.QApplication(sys.argv)
- ui = Ui_MainWindow()
- ui.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement