def on_conn_fail(self) -> None: """ If the API failed to connect, the app will be closed. """ print("Timed out waiting for the connection") QCoreApplication.exit(1)
def wait_for_connection(self) -> None: """ Function called by start() to check every second if the connection has been established. """ # Saving the starting timestamp for the audiosync feature start_time = time.time() # Changing the loading message for the connection one if the first # connection attempt was unsuccessful. if self.conn_counter == 1: self.layout.removeWidget(self.loading_label) self.loading_label.hide() self.conn_label.show() # The APIs should raise `ConnectionNotReady` if the first attempt # to get metadata from Spotify was unsuccessful. logging.info("Connection attempt %d", self.conn_counter + 1) try: self.conn_fn() except ConnectionNotReady: pass else: logging.info("Succesfully connected to the API") # Stopping the timer and changing the label to the loading one. self.conn_timer.stop() self.layout.removeWidget(self.conn_label) del self.conn_timer self.layout.removeWidget(self.conn_label) self.conn_label.hide() del self.conn_label self.layout.removeWidget(self.loading_label) del self.loading_label # Loading the player and more self.setStyleSheet(f"background-color:{Colors.black};") self.layout.addWidget(self.player) self.play_video(self.api.artist, self.api.title, start_time) # Connecting to the signals generated by the API self.api.new_song_signal.connect(self.play_video) self.api.position_signal.connect(self.change_video_position) self.api.status_signal.connect(self.change_video_status) # Starting the event loop if it was initially passed as # a parameter. if self.event_loop_interval is not None: self.start_event_loop(self.api.event_loop, self.event_loop_interval) self.conn_counter += 1 # If the maximum amount of attempts is reached, the app is closed. if self.conn_counter >= self.conn_attempts: print("Timed out waiting for the connection") self.conn_timer.stop() QCoreApplication.exit(1)
def test_event_engine2_registerGeneralHandler(self): def simpletest(event): print(u'registerGeneralHandler处理每秒触发的计时器事件:{}'.format( str(datetime.now()))) app = QCoreApplication(sys.argv) ee = EventEngine2() ee.registerGeneralHandler(simpletest) ee.start() sleep(3) ee.stop() app.exit(0)
def test_event_engine2_register(self): def simpletest(event): print(u'eventEngine2处理每秒触发的计时器事件:{}'.format(str(datetime.now()))) app = QCoreApplication(sys.argv) ee = EventEngine2() # 不触发定时器 # ee.register(1, simpletest) ee.register(EVENT_TIMER, simpletest) ee.start() sleep(3) ee.stop() app.exit(0)