PyQt Widget connect() and disconnect()

2020-02-19 05:28发布

Depending on a conditions I would like to connect/re-connect a button to a different function.

Let's say I have a button:

myButton = QtGui.QPushButton()

For this example let's say I check if there is an internet connection.

if connected == True:
    myButton.clicked.connect(function_A)

elif connected == False:
    myButton.clicked.connect(function_B)

First of all I would like to disconnect a button from any function it was already connected before the button is being re-assigned/re-connected to another function (function_A or function_B). Secondly, I have already noticed that after the button is re-connected it takes an extra click for the button to pick up a new function. After the button is re-connected to another function it still attempts to run a previous function - a function to which a button was connected earlier (before a re-connection). Please advice. Thanks in advance!

EDITED LATER:

It appears a widget's .disconnect() method can be used to disconnect a button from a function it it is connected.

myButton.disconnect()

Unfortunately .disconnect() throws an error if a widget is not connected to any function. To get around it I am using Try/Except. But I would rather use a more elegant solution...

try: myButton.clicked.disconnect() 
except Exception: pass

2条回答
Lonely孤独者°
2楼-- · 2020-02-19 06:23

Try this:

from PyQt4 import QtGui as gui

app = gui.QApplication([])

myButton = gui.QPushButton()

def function_A():
    myButton.clicked.disconnect() #this disconnect all!
    myButton.clicked.connect(function_B)
    print 'function_A'

def function_B():
    myButton.clicked.disconnect(function_B) #this disconnect function_B
    myButton.clicked.connect(function_A)
    print 'function_B'

myButton.clicked.connect(function_A)
myButton.setText("Click me!")
myButton.show()

app.exec_()
查看更多
Luminary・发光体
3楼-- · 2020-02-19 06:32

If you need to reconnect signals in many places, then you could define a generic utility function like this:

def reconnect(signal, newhandler=None, oldhandler=None):
    while True:
        try:
            if oldhandler is not None:
                signal.disconnect(oldhandler)
            else:
                signal.disconnect()
        except TypeError:
            break
    if newhandler is not None:
        signal.connect(newhandler)

...

if connected:
    reconnect(myButton.clicked, function_A)
else:
    reconnect(myButton.clicked, function_B)

(NB: the loop is needed for safely disconnecting a specific handler, because it may have been connected multple times, and disconnect only removes one connection at a time.).

查看更多
登录 后发表回答