Matan
Matan

Reputation: 105

How to get a return value from a connect function

I want to get the return value of the checkFinish Function, the purpose is to do 'something' if the value I get is False, is there a way to get the return value of a function started by the connect function? (asking about the button at the same time because it's probably the same way.)

code:

from PyQt5 import QtWidgets, uic
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import *
from functools import partial

registered = False
loginClick = False

def login():
    global loginClick
    loginClick = True


def checkFinish():
    global registered, loginClick
    if loginClick or registered:
        return False
    else: 
        return True


def register_form():
    global registered, loginClick
    app = QtWidgets.QApplication([])
    dlg = uic.loadUi("registerForm.ui")
    dlg.usernameLine.setFocus()
    dlg.logInButton.clicked.connect(login)
    dlg.timer = QTimer(dlg, interval=5)
    dlg.timer.timeout.connect(checkFinish) # Get the return value from here.
    #if dlg.timer.timeout.connect(checkFinish): do something example
    dlg.timer.start()
    dlg.show()
    app.exec()

Let's say I click the logInButton, the login function will change the value of loginClick to True and the Function checkFinish that runs in the background because of the timer will return False, the problem again is how to get that False value in the register_form function where I start the timer.

Upvotes: 1

Views: 1343

Answers (1)

eyllanesc
eyllanesc

Reputation: 244202

The signal only serves to invoke a function and pass it some parameters so you cannot get the result of the function invoked. The correct logic is to invoke another function that evaluates the initial function:

# ...
dlg.timer = QTimer(dlg, interval=5)
def onTimeout():
    if checkFinish():
        print("finish")
dlg.timer.timeout.connect(onTimeout)
dlg.timer.start()
# ...

Upvotes: 2

Related Questions