PyQt Widget connect() and disconnect()

前端 未结 2 1027
时光说笑
时光说笑 2020-12-24 02:53

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.QPushButt         


        
2条回答
  •  一生所求
    2020-12-24 03: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.).

提交回复
热议问题