PyQt Widget connect()和disconnect() [英] PyQt Widget connect() and disconnect()

查看:1162
本文介绍了PyQt Widget connect()和disconnect()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据条件,我想将按钮连接/重新连接到不同的功能。



假设我有一个按钮:

  myButton = QtGui.QPushButton()

对于这个例子,假设我检查是否有互联网连接。

 如果连接== True:
myButton.clicked.connect(function_A)

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

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



已编辑:



出现小部件 .disconnect strong>方法可用于从其所连接的功能断开按钮。

  myButton.disconnect()

不幸的是,如果一个小部件没有连接到任何函数,disconnect()会抛出一个错误。
为了绕过它,我使用Try / Except。但我宁愿使用更优雅的解决方案...

  try:myButton.clicked.disconnect例外:pass 


解决方案

如果您需要重新连接信号那么你可以这样定义一个通用的实用函数:

  def reconnect(signal,newhandler = None,oldhandler = None) :
while True:
try:
如果oldhandler不是None:
signal.disconnect(oldhandler)
else:
signal.disconnect()
,除了TypeError:
break
如果newhandler不是None:
signal.connect(newhandler)

...

如果连接:
reconnect(myButton.clicked,function_A)
else:
reconnect(myButton.clicked,function_B)

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


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

解决方案

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

这篇关于PyQt Widget connect()和disconnect()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆