Python Forum
[PyQt] No reaction and no error message when clicking button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] No reaction and no error message when clicking button
#2
The problem is that your subwindow must be owned by another object, else it is immediatly destroyed by the garbage collector. Therefore, this works (see the 'show_concentration_test_window' method):

#!/usr/bin/python3
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):
    def __init__(self, parent=None):
        super().__init__()
        loadUi("mainwindow.ui", self)
        self.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}
        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):
        self.concentration_test = ConcentrationTest(self.COLORS, self.get_designated_color())
        self.concentration_test_window = ConcentrationTestWindow(self.concentration_test, self.get_switch_time())
        self.concentration_test_window.show()
        self.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()
From a quick look, the structure would also need to be reviewed a bit, i.e. why declare concentration_test here
def __init__(self, concentration_test, parent=None):
as when you init the MainWindow, only app is passed
    main_window = MainWindow(app)
Reply


Messages In This Thread
RE: No reaction and no error message when clicking button - by Alfalfa - Nov-21-2018, 04:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] error message the_wolf_dz 4 2,067 Oct-24-2022, 07:24 PM
Last Post: deanhystad
  [Tkinter] Clicking on the button crashes the TK window ODOshmockenberg 1 2,258 Mar-10-2022, 05:18 PM
Last Post: deanhystad
  [Tkinter] Button error Tyrel 2 2,018 Jun-20-2021, 07:21 AM
Last Post: Tyrel
  Error message box and quit app Kumarkv 1 2,258 May-19-2020, 07:05 PM
Last Post: Larz60+
  [Tkinter] How to make message box error stay on top of window scratchmyhead 1 8,322 May-10-2020, 10:21 PM
Last Post: scratchmyhead
  Need tkinter help with clicking buttons pythonprogrammer 2 2,485 Jan-03-2020, 04:43 AM
Last Post: joe_momma
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,042 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] RE: status bar to return to the centre after 1 minute of clicking a button ? chano 6 4,732 May-27-2019, 04:24 PM
Last Post: Yoriz
  tkinter- adding a new window after clicking a button built on the gui ShashankDS 2 6,645 Apr-18-2019, 12:48 PM
Last Post: ShashankDS
  [Tkinter] Adding New TAB to NoteBook Widget by Clicking Vicolas 0 2,632 Feb-15-2019, 06:03 PM
Last Post: Vicolas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020