Python PyQt Widget connect() 和 disconnect()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21586643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:12:45  来源:igfitidea点击:

PyQt Widget connect() and disconnect()

pythonpyqtclicksignals-slotsdisconnect

提问by alphanumeric

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!

首先,在按钮被重新分配/重新连接到另一个功能(function_A 或 function_B)之前,我想将按钮与它已经连接的任何功能断开连接。其次,我已经注意到,重新连接按钮后,需要额外单击按钮才能选择新功能。按钮重新连接到另一个功能后,它仍会尝试运行前一个功能 - 按钮先前(重新连接之前)连接到的功能。请指教。提前致谢!

EDITED LATER:

稍后编辑:

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

看起来小部件的.disconnect()方法可用于断开按钮与其连接的功能的连接。

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

不幸的是,如果小部件未连接到任何函数, .disconnect() 会引发错误。为了解决这个问题,我使用了 Try/Except。但我宁愿使用更优雅的解决方案......

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

采纳答案by ekhumoro

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 disconnectonly removes one connection at a time.).

(注意:安全断开特定处理程序需要循环,因为它可能已连接多次,并且一次disconnect仅删除一个连接。)。

回答by Alvaro Fuentes

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_()