English 中文(简体)
PyQt Widgetlink()和断电
原标题:PyQt Widget connect() and disconnect()

取决于条件 我想把一个纽子连接起来,与另一个功能连接起来。

我要说的是:

myButton = QtGui.QPushButton()

例如,我要说,如果存在互联网联系,我会检查。

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:

这种方法似乎是一种植被()。 脱节()方法可用于将丁子与其相关的功能分离。

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
最佳回答

如果你需要在许多地方重新连接信号,那么你可以确定一种通用的通用功能,如:

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

...

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

(NB:由于可能已经连接了干燥的时间,因此有必要安全脱节,而<代码>脱节(位置)只取消了一段时间的一条连接。)

问题回答

为此:

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

与此类似:

button.doubleClicked.connect(my_function)

它检查了纽特是否相互关联,如果是:

if button.doubledClicked.connect(my_function):
     button.doubledClicked.disconnect()




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签