user3353373
user3353373

Reputation: 13

python pyqt interactive usuage

I would like to use edit the functions defining the slots while my pyqt gui is running so that I can debug the program without having to restart it. An example of what I have is, after importing the py file generated by pyuic

##-------------------------##

app=QApplication([])
window = QMainWindow()
window.show()
ui = Ui_MainWindow()
ui.setupUi(window)
##-------------------------##
def helloworld():
     print "hi"

ui.pushButton.clicked.connect(helloworld)

Now, after starting this program I can execute commands in the console. What I would like to do is after running the program, redefine the helloworld function using the console

def helloworld():
    print "goodbye"

However, if I do this, both "hi" and "goodbye" are both printed. I would like only for "goodbye" to be printed.

I am going to use this feature to debug my functions, while the gui is live. I am using spyder IDE with python 2.7.6 and pyqt4

EDIT:

After redefining the function, I reconnect the slot. Thereby, the clicked action results in executing the old function (printing "hi") and the new function (printing "goodbye")

Upvotes: 1

Views: 78

Answers (1)

three_pineapples
three_pineapples

Reputation: 11869

Redefining the function does not disconnect the original signal and does not destroy the original function. It just creates a new function with the same name, preventing access to the original function by anything which does not already hold a reference to it. As Qt already has a reference, both functions are called (I assume you connect your new function to the button?)

One way to solve this is to explicitly call ui.pushButton.clicked.disconnect(helloworld) before redefining the function.

Alternatively, wrap your function in another:

def wrap(*args, **kwargs):
    return helloworld(*args, **kwargs)

ui.pushButton.clicked.connect(wrap)

Then redefining helloworld will have the desired effect.

Upvotes: 1

Related Questions