Collaptic
Collaptic

Reputation: 307

How to connect PyQt signal to external function

How does one connect a pyqt button signal in one file, to a function in another python file? I've tried various things, but nothing seems to work. This is the first file:

from PyQt4 import QtGui
from PyQt4.QtGui import QMainWindow
from MainUIFile import Ui_Main
from pythonfile import myOutsideFunction
class MainWindow(QMainWindow,Ui_file):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.btn.clicked.connect(myOutsideFunction())

The second file that is called by the first:

def myOutsideFunction(self):
    # Do some stuff here

How would I go about doing this?

Upvotes: 1

Views: 2138

Answers (2)

user2070870
user2070870

Reputation: 101

What is the importance of connecting to a function outside of your code? Could you not just do something like this:

def myOutsideFunction(a,b,c,d):
    #process variables/objects a,b,c,d as you wish
    return answer

from PyQt4 import QtGui
from PyQt4.QtGui import QMainWindow
from MainUIFile import Ui_Main
from pythonfile import myOutsideFunction
class MainWindow(QMainWindow,Ui_file):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.btn.clicked.connect(self.pressed_the_button())
        #initialize your variables/objects for your outside function if need be

    def pressed_the_button(self):
        answer_from_outside_function = myOutsideFunction(self.a,self.b,self.c,self.d)
        # run whatever you need to here...

Upvotes: 1

user3419537
user3419537

Reputation: 5000

You are currently making a call to myOutsideFunction and passing the result to the connect function, which expects a callable as an argument.

Remove the parenthesis from myOutsideFunction in the connect call

self.btn.clicked.connect(myOutsideFunction)

Upvotes: 2

Related Questions