Nov-20-2018, 11:19 PM
Hi
I have no idea why nothing happens after clicking on "Start Test" in the given Python 3 (PyQt5) code. There appears also no error message.
mainwindow.ui:
I have no idea why nothing happens after clicking on "Start Test" in the given Python 3 (PyQt5) code. There appears also no error message.
mainwindow.ui:
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>350</width> <height>250</height> </rect> </property> <property name="windowTitle"> <string>Color Concentration Test</string> </property> <widget class="QWidget" name="centralwidget"> <layout class="QGridLayout" name="gridLayout_2"> <item row="0" column="0"> <layout class="QVBoxLayout" name="verticalLayout"> <item> <spacer name="verticalSpacer_2"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>20</width> <height>40</height> </size> </property> </spacer> </item> <item> <widget class="QGroupBox" name=""> <layout class="QGridLayout" name="gridLayout"> <item row="1" column="1"> <widget class="QSpinBox" name="spinBox_switch_time"> <property name="maximum"> <number>100000</number> </property> <property name="value"> <number>1000</number> </property> </widget> </item> <item row="1" column="0"> <widget class="QLabel" name="label_switch_time"> <property name="text"> <string>Switch Time (ms):</string> </property> <property name="alignment"> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> </property> </widget> </item> <item row="0" column="1"> <widget class="QComboBox" name="comboBox_designated_color"> <property name="currentText"> <string/> </property> </widget> </item> <item row="0" column="0"> <widget class="QLabel" name="label_designated_color"> <property name="text"> <string>Designated Color:</string> </property> <property name="alignment"> <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> </property> </widget> </item> </layout> </widget> </item> <item> <spacer name="verticalSpacer"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>20</width> <height>40</height> </size> </property> </spacer> </item> <item> <widget class="QPushButton" name="pushButton_start_test"> <property name="layoutDirection"> <enum>Qt::LeftToRight</enum> </property> <property name="text"> <string>Start Test</string> </property> </widget> </item> </layout> </item> </layout> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>350</width> <height>28</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/> </ui>main.py:
import sys import time from random import choice from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.uic import loadUi from PyQt5 import QtCore class ConcentrationTest(object): def __init__(self, COLORS, designated_color): self.COLORS = COLORS self.designated_color = designated_color self.start_time = None self.color = None self.color_locked = True self.reaction_times = [] self.false_positives = 0 self.color_missed = 0 self.key_too_often_pressed = 0 def space_pressed(self): if self.color == self.designated_color: if self.color_locked: self.key_too_often_pressed += 1 else: reaction_time = time.monotonic() - self.start_time self.reaction_times.append(reaction_time) self.color_locked = True else: self.false_positives += 1 def next_round(self): if not self.color_locked and self.color == self.designated_color: self.color_missed += 1 self.color_locked = False current_color = self.color while self.color == current_color: self.color = choice(list(self.COLORS.values())) self.start_time = time.monotonic() return self.color class MainWindow(QMainWindow): COLORS = {"Black": QtCore.Qt.black, "Blue": QtCore.Qt.blue, "Cyan": QtCore.Qt.cyan, "Green": QtCore.Qt.green, "Magenta": QtCore.Qt.magenta, "Red": QtCore.Qt.red, "Yellow": QtCore.Qt.yellow} def __init__(self, concentration_test, parent=None): super().__init__(parent) loadUi("mainwindow.ui", self) self.concentration_test = concentration_test self.comboBox_designated_color.addItems(self.COLORS) self.pushButton_start_test.clicked.connect(self.show_concentration_test_window) def get_designated_color(self): return self.COLORS[self.comboBox_designated_color.currentText()] def get_switch_time(self): return self.spinBox_switch_time.value() def show_concentration_test_window(self): concentration_test = ConcentrationTest(self.COLORS, self.get_designated_color()) concentration_test_window = ConcentrationTestWindow(concentration_test, self.get_switch_time()) concentration_test_window.show() concentration_test_window.change_screen_color() class ConcentrationTestWindow(QMainWindow): def __init__(self, concentration_test, switch_time, parent=None): super().__init__(parent) self.showFullScreen() self.concentration_test = concentration_test self.switch_time = switch_time self.timer = QtCore.QTimer() self.timer.timeout.connect(self.change_screen_color) self.pal = self.palette() self.setAutoFillBackground(True) def change_screen_color(self): color = self.concentration_test.next_round() self.pal.setColor(self.backgroundRole(), color) self.setPalette(self.pal) self.timer.start(self.switch_time) def keyPressEvent(self, event): if event.key() == QtCore.Qt.Key_Escape: self.close() if event.key() == QtCore.Qt.Key_Space: self.concentration_test.space_pressed() def main(): app = QApplication(sys.argv) main_window = MainWindow(app) main_window.show() sys.exit(app.exec_()) if __name__ == "__main__": main()What is the reason for this?