python3.4 + PyQt5 - How to connect signals between different files

205 Views Asked by At

I am working with python3.4 and PyQt5 recently. It seems too large to put all the code in one .py file, and not convenient to be extended in future. So I am trying to split it into several files.

Unfortunately, I have some troubles in connecting signals.

Here's the example for my code:

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow
from Ui_BA import Ui_MainWindow
from PyQt5.QtGui import *
import AA, sys

class BA(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(BA, self).__init__(parent)
        self.setupUi(self)
        a = AA.AAUI.search_button_released(self)
        self.aa_search_button.released.connect(lambda: a)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    dlg = BA()
    dlg.show()
    sys.exit(app.exec_())

And AA.py is like this:

from PyQt5.QtWidgets import QMainWindow
from Ui_BA import Ui_MainWindow

class AAUI(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(AAUI, self).__init__(parent)
        self.setupUi(self)
    def search_button_released(self):
        self.statusBar.showMessage('BlaBlaBla')

When I run the main py, the statusBar shows the message automatically, rather than after clicking on the search button.

Thank you for reading. Please help.

1

There are 1 best solutions below

1
On

First of all you should not split your project this way. In my own works I tried to separate each class in each file, not split the one class code between several files. In the future you will able to have a lot of problems (it's just my opinion!)

In your case the string a = AA.AAUI.search_button_released(self) automatically executes the function. You should remove it and replace your lambda:

self.aa_search_button.released.connect(lambda: AA.AAUI.search_button_released(self))

It'll help!