Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. from customWidgets import *
  2. from PyQt5 import uic
  3. from PyQt5.QtCore import *
  4. from PyQt5.QtGui import *
  5. from PyQt5.QtWidgets import *
  6.  
  7.  
  8. QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
  9. app = QApplication([])
  10.  
  11. window = uic.loadUi("experimentThree.ui")
  12. file = open('all_results.csv', 'w')
  13. file.write("age, gender, education, race\n") # Headers of data file
  14. window.consent_error_lbl.hide()
  15. window.o_gender.hide()
  16. window.o_race.hide()
  17.  
  18.  
  19. def next_page(): # Takes user to the next page
  20. current_page = window.stackedWidget.currentIndex()
  21. if current_page == 0:
  22. consent_checker()
  23. elif current_page == 1:
  24. demographics_checker()
  25. else:
  26. window.stackedWidget.setCurrentIndex(current_page + 1)
  27.  
  28.  
  29. def consent_checker(): # Confirms user's consent and that they understand the criteria to do the study
  30. current_page = window.stackedWidget.currentIndex()
  31. if not window.checkBox_1.isChecked() or not window.checkBox_2.isChecked() or not window.checkBox_3.isChecked() \
  32. or not window.checkBox_4.isChecked() or not window.checkBox_5.isChecked() or not \
  33. window.checkBox_6.isChecked():
  34. window.consent_error_lbl.show() # If not all clicked when clicking 'next', error appears
  35. else:
  36. window.stackedWidget.setCurrentIndex(current_page + 1) # Next page, when user fully consents
  37.  
  38.  
  39. def demographics_checker(): # Confirms all demographic information inputted by the user is appropriate
  40. error_demographics = []
  41. current_page = window.stackedWidget.currentIndex()
  42.  
  43. if other.isChecked(): # Provides text boxes if user's gender and racial identity aren't immediate options
  44. window.o_gender.show()
  45. else:
  46. window.o_gender.hide()
  47. if window.race.currentText() == "Other":
  48. window.o_race.show()
  49. else:
  50. window.o_race.hide()
  51.  
  52. if window.age.value() < 18: # Demographic confirmation
  53. error_demographics.append("age")
  54. if (not male.isChecked() and not female.isChecked() and not other.isChecked()) \
  55. or (other.isChecked() and window.o_gender.text() == ""):
  56. error_demographics.append("gender")
  57. if window.race.currentText() == "---" or (window.race.currentText() == "Other" and window.o_race.text() == ""):
  58. error_demographics.append("race")
  59. if not school.isChecked() and not undergrad.isChecked() and not masters.isChecked() and not phd.isChecked():
  60. error_demographics.append("education level")
  61.  
  62. if len(error_demographics) > 0:
  63. window.demographics_error_lbl.setText(f"ERROR: Please input an appropriate value for the following "
  64. f"demographics: \n\n {', '.join(error_demographics)}")
  65. else:
  66. window.demographics_error_lbl.hide()
  67. if male.isChecked(): # Storing chosen gender
  68. gender = 'Male'
  69. elif female.isChecked():
  70. gender = 'Female'
  71. elif other.isChecked():
  72. gender = window.o_gender.text()
  73.  
  74. if school.isChecked(): # Storing chosen education level
  75. education = 'High School'
  76. elif undergrad.isChecked():
  77. education = 'Undergraduate Degree'
  78. elif masters.isChecked():
  79. education = 'Masters Degree'
  80. elif phd.isChecked():
  81. education = 'PhD'
  82.  
  83. if window.race.currentText() != "Other": # Writing demographic information into data file
  84. file.write(f'{window.age.value()}, {gender}, {education}, {window.race.currentText()}')
  85. else:
  86. file.write(f'{window.age.value()}, {gender}, {education}, {window.o_race.text()}')
  87. file.close()
  88. window.stackedWidget.setCurrentIndex(current_page + 1) # Next page, when appropriate demographics inputted
  89.  
  90.  
  91. window.next_button.clicked.connect(next_page)
  92. # Gender demographics
  93. male = colouredButton(window.gender_group)
  94. male.setStyle(0, 0, 400, 50, 'Male')
  95. male.setIcon(QIcon('male.png'))
  96. female = colouredButton(window.gender_group)
  97. female.setStyle(410, 0, 400, 50, 'Female')
  98. female.setIcon(QIcon('female.png'))
  99. other = colouredButton(window.gender_group)
  100. other.setStyle(0, 60, 400, 50, 'Other')
  101.  
  102. # Education level demographics
  103. school = colouredButton(window.education_group)
  104. school.setStyle(0, 0, 400, 50, 'High School')
  105. undergrad = colouredButton(window.education_group)
  106. undergrad.setStyle(410, 0, 400, 50, 'Undergraduate Degree')
  107. masters = colouredButton(window.education_group)
  108. masters.setStyle(0, 60, 400, 50, 'Masters Degree')
  109. phd = colouredButton(window.education_group)
  110. phd.setStyle(410, 60, 400, 50, 'PhD')
  111.  
  112.  
  113. def dot_updirection():
  114. window.dot.show()
  115. current_y = window.dot.y()
  116. if window.dot.y() == 250:
  117. window.dot.hide()
  118. window.dot.setGeometry(540, 300, 20, 20)
  119. else:
  120. window.dot.setGeometry(540, current_y - 10, 20, 20)
  121.  
  122.  
  123. def dot_leftdirection():
  124. window.dot.show()
  125. current_x = window.dot.x()
  126. if window.dot.x() == 490:
  127. window.dot.hide()
  128. window.dot.setGeometry(540, 300, 20, 20)
  129. else:
  130. window.dot.setGeometry(current_x - 10, 300, 20, 20)
  131.  
  132.  
  133. def dot_rightdirection():
  134. window.dot.show()
  135. current_x = window.dot.x()
  136. if window.dot.x() == 590:
  137. window.dot.hide()
  138. window.dot.setGeometry(540, 300, 20, 20)
  139. else:
  140. window.dot.setGeometry(current_x + 10, 300, 20, 20)
  141.  
  142.  
  143. def dot_downdirection():
  144. window.dot.show()
  145. current_y = window.dot.y()
  146. if window.dot.y() == 350:
  147. window.dot.hide()
  148. window.dot.setGeometry(540, 300, 20, 20)
  149. else:
  150. window.dot.setGeometry(540, current_y + 10, 20, 20)
  151.  
  152.  
  153. window.dot.hide()
  154. timerUP = QTimer()
  155. timerUP.timeout.connect(dot_updirection)
  156. timerDOWN = QTimer()
  157. timerDOWN.timeout.connect(dot_downdirection)
  158. timerLEFT = QTimer()
  159. timerLEFT.timeout.connect(dot_leftdirection)
  160. timerRIGHT = QTimer()
  161. timerRIGHT.timeout.connect(dot_rightdirection)
  162.  
  163.  
  164. def dot_upmotion():
  165. timerUP.start(100)
  166.  
  167.  
  168. def dot_leftmotion():
  169. timerLEFT.start(100)
  170.  
  171.  
  172. def dot_rightmotion():
  173. timerRIGHT.start(100)
  174.  
  175.  
  176. def dot_downmotion():
  177. timerDOWN.start(100)
  178.  
  179.  
  180. # Adding keyboard key images to screen; connecting click of image to respective exemplar motion of dot
  181. i = clickLabel(window.page_3)
  182. i.setStyle('i_key.png', 140, 240, 60, 60)
  183. i.clicked.connect(dot_upmotion)
  184. j = clickLabel(window.page_3)
  185. j.setStyle('j_key.png', 90, 300, 60, 60)
  186. j.clicked.connect(dot_leftmotion)
  187. k = clickLabel(window.page_3)
  188. k.setStyle('k_key.png', 180, 300, 60, 60)
  189. k.clicked.connect(dot_rightmotion)
  190. m = clickLabel(window.page_3)
  191. m.setStyle('m_key.png', 130, 360, 60, 60)
  192. m.clicked.connect(dot_downmotion)
  193.  
  194.  
  195. window.show()
  196. app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement