Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. import sys
  2. from PyQt5 import QtCore, QtGui, QtWidgets
  3. from PyQt5.QtGui import QColor
  4. from PyQt5.QtCore import Qt, QEvent, QObject
  5. import random
  6.  
  7. class UserInputClass(QtWidgets.QLineEdit):
  8. def __init__(self):
  9. QtWidgets.QLineEdit.__init__(self)
  10.  
  11. def keyPressEvent(self, keyEvent):
  12. super(UserInputClass, self).keyPressEvent(keyEvent)
  13. if keyEvent.key() == Qt.Key_Backspace:
  14. print('Backspace pressed')
  15.  
  16.  
  17. class Ui_MainWindow(QtWidgets.QMainWindow):
  18. def __init__(self):
  19. super().__init__()
  20. self.initUI()
  21.  
  22. def initUI(self):
  23. self.setWindowTitle("TypeJam")
  24. self.centralwidget = QtWidgets.QWidget(self)
  25. self.setCentralWidget(self.centralwidget)
  26. self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
  27. self.setGeometry(300, 300, 800, 500)
  28. self.initUserInput()
  29. self.initTextForTyping()
  30. self.initButtons()
  31. self.addLayouts()
  32. self.inputProcessing()
  33.  
  34. def initTextForTyping(self):
  35. self.textForTyping = QtWidgets.QTextEdit(self)
  36. self.textForTyping.setGeometry(30, 30, 580, 280)
  37. fontTextForTyping = QtGui.QFont()
  38. fontTextForTyping.setFamily("Arial")
  39. fontTextForTyping.setPointSize(20)
  40. self.textForTyping.setFont(fontTextForTyping)
  41. self.textForTyping.setReadOnly(True)
  42. self.textForTyping.setText(self.GetTextFromFile())
  43. self.str_textForTyping = self.textForTyping.toPlainText()
  44.  
  45. def initUserInput(self):
  46. self.userInput = UserInputClass()
  47. self.userInput.setGeometry(30, 320, 580, 40)
  48. fontUserInput = QtGui.QFont()
  49. fontUserInput.setFamily("Arial")
  50. fontUserInput.setPointSize(20)
  51. self.userInput.setFont(fontUserInput)
  52. self.str_userInput = str()
  53. self.userInput.textChanged.connect(self.inputProcessing)
  54. # self.userInput.returnPressed.connect(self.backspace)
  55. # self.kpa = self.KeyPressEater()
  56. # self.userInput.installEventFilter(self.kpa)
  57.  
  58. def initButtons(self):
  59. self.buttonStart = QtWidgets.QPushButton("Start", self)
  60. self.buttonStop = QtWidgets.QPushButton("Stop", self)
  61.  
  62. def addLayouts(self):
  63. self.gridLayout.addWidget(self.textForTyping, 0, 0, 1, 1)
  64. self.gridLayout.addWidget(self.userInput, 1, 0, 1, 1)
  65. self.verticalLayout = QtWidgets.QVBoxLayout()
  66. self.verticalLayout.addWidget(self.buttonStart)
  67. self.verticalLayout.addWidget(self.buttonStop)
  68. self.spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
  69. self.verticalLayout.addItem(self.spacerItem)
  70. self.gridLayout.addLayout(self.verticalLayout, 0, 1)
  71.  
  72. def GetTextFromFile(self):
  73. textfile = open("text.txt", "r")
  74. self.TextFromFile = textfile.read()
  75. textfile.close()
  76. return self.TextFromFile
  77.  
  78. # class KeyPressEater(QObject):
  79. # def eventFilter(self, obj, event):
  80. # if event.type() == QEvent.KeyPress:
  81. # print("Ate key press", event.key())
  82. # return True
  83. # else:
  84. # # standard event processing
  85. # return QObject.eventFilter(self, obj, event)
  86.  
  87. # def eventFilter(self, obj, event):
  88. # if obj == self.userInput:
  89. # if event.type() == QEvent.KeyPress:
  90. # print ("Ate key press", event.key())
  91. # return True
  92. # else:
  93. # return False
  94. # else:
  95. # return QtWidgets.QMainWindow.eventFilter(self, obj, event)
  96. # def keyPressEvent(self, event):
  97. # if event.key() == Qt.Key_Backspace:
  98. # print("back")
  99.  
  100. # def backspace(self):
  101. # print("backback")
  102.  
  103. # def keyPressEvent(self, QKeyEvent):
  104. # if QKeyEvent.key() == Qt.Key_Backspace:
  105. # print("backspace")
  106.  
  107. def inputProcessing(self):
  108. current_userInput = self.userInput.text()
  109. if len((self.userInput.text())) != 0:
  110. lastSymbol = self.userInput.text()[len(self.userInput.text()) - 1]
  111. numberOfRight = 1
  112. if self.str_textForTyping.find(self.str_userInput + lastSymbol) == 0:
  113. self.str_userInput += lastSymbol
  114. numberOfRight += 1
  115. else:
  116. # self.textForTyping.setStyleSheet("background: rgb(120,120,120)")
  117. print(self.str_userInput)
  118.  
  119. if " " in self.userInput.text():
  120. self.userInput.clear()
  121.  
  122.  
  123.  
  124. if __name__ == "__main__":
  125. app = QtWidgets.QApplication(sys.argv)
  126. ui = Ui_MainWindow()
  127. ui.show()
  128.  
  129. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement