0

I have created 2 new widgets/windows in pyqt, from my my main window. Now I would like to access my methods in mainwindow class to my new widgets. How can I do this?

Here is my code:

from UI_NewProject import Ui_widget
from UI_OpenNew import Ui_Form

# Main Class
class MainClass(QtGui.QMainWindow, UI_Main.Ui_MainWindow):
def __init__(self, parent=None):
    super(MainClass, self).__init__(parent)
    self.setupUi(self)
    self.openFile.triggered.connect(self.on_pushButton_clicked)

def on_pushButton_clicked(self):
    connectClass = openNew(self)
    connectClass.show()

def funcfromMain(self):
    filters = ('Data Files (*.csv *.txt *.xls *.xml *.xlsx *.xlsm)',)
    path, filter = QtGui.QFileDialog.getOpenFileNameAndFilter(
        self, 'Open File', '', ';;'.join(filters))
    self.nameFile = os.path.basename(path)

    if (".csv" or ".txt") in path:
        with open(path, 'rb') as drate:
            self.Datas = pd.read_csv(drate, index_col=0)


    if (".xls" or ".xml" or ".xlsx" or ".xlsm") in path:
        with open(path, 'rb') as drate:
            self.Datas = pd.read_excel(drate, index_col=0)


#New Widget/Window class 1
class openNew(QtGui.QMainWindow, Ui_Form):
    #from UI_OpenNew import Ui_Form

    def __init__(self, parent = None):
        super(openNew, self).__init__(parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setupUi(self)

        # Create New Project
        self.pushButton_2.clicked.connect(self.on_Button_clicked)
        self.pushButton.clicked.connect(MainClass.funcfromMain) #this is funtion in MainClass and I want to access it Here

    def on_Button_clicked(self):
        Win = NewProject(self)
        Win.show()

#New Widget/Window class 2
class NewProject(QtGui.QMainWindow, Ui_widget):
    #from UI_NewProject import Ui_widget

    def __init__(self, parent = None):
        super(NewProject, self).__init__(parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setupUi(self)

self.pushButton_2 and self.pushButton are from the corresponding UI files.

Now I want to connect buttons like these in the New UI files to methods in MainClass, So any Ideas on how I can achieve this?

Error: (with parent.func)

TypeError: QFileDialog.getOpenFileNameAndFilter(QWidget parent=None, str caption='', str directory='', str filter='', str initialFilter='', QFileDialog.Options options=0) -> (str, str): argument 1 has unexpected type 'bool'

Update 1: I tried using Mixin Class but my main file is so big and I want like 6-7 methods of main in the new widgets so, any ideas of how I can approach this?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
learncode
  • 1,105
  • 4
  • 18
  • 36
  • what errors ? we can't read in you mind. Always add in question full error message (Traceback). – furas Dec 28 '16 at 16:55
  • did you try to use `parent` - ie. `parent.funcfromMain` – furas Dec 28 '16 at 16:59
  • @eyllanesc, Yes, I have read through the concepts now I am wondering if I cannot do this at all, I just posted this hoping to receive any Ideas of how to approach this! – learncode Dec 28 '16 at 17:00
  • @furas, Yes I tried 'parent.funcfromMain' it just passes 'bool' to the function I don't know why it is passing 'bool'! – learncode Dec 28 '16 at 17:09
  • your `funcfromMain` has only `pass` so how do you know that it passed `bool` to function ? – furas Dec 28 '16 at 17:29
  • No it does not just have bool it has lot some code to open a csv and excel file but I just put `pass` to it in stackoverflow! I will add that in a sec – learncode Dec 28 '16 at 17:40
  • add all error messages - it can be more usefull than your descriptions. – furas Dec 28 '16 at 17:42

1 Answers1

1

In MainClass you must make the connection: connectClass.pushButton.clicked.connect(self.funcfromMain)

from PyQt4 import QtGui


from UI_NewProject import Ui_widget
from UI_OpenNew import Ui_Form

# New Widget/Window class 2
class NewProject(QtGui.QMainWindow, Ui_widget):
    # from UI_NewProject import Ui_widget

    def __init__(self, parent=None):
        super(NewProject, self).__init__(parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setupUi(self)


# New Widget/Window class 1
class openNew(QtGui.QMainWindow, Ui_Form):
    # from UI_OpenNew import Ui_Form

    def __init__(self, parent=None):
        super(openNew, self).__init__(parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setupUi(self)

        # Create New Project
        self.pushButton_2.clicked.connect(self.on_Button_clicked)

    def on_Button_clicked(self):
        Win = NewProject(self)
        Win.show()


# Main Class
class MainClass(QtGui.QMainWindow, UI_Main.Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainClass, self).__init__(parent)
        self.setupUi(self)
        self.openFile.triggered.connect(self.on_pushButton_clicked)

    def on_pushButton_clicked(self):
        connectClass = openNew(self)
        connectClass.pushButton.clicked.connect(self.funcfromMain)
        connectClass.show()

    def funcfromMain(self):
        pass
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks!, Yeah the New Widget 1 works but what about the New Widget 2, the line `Win.show()` it just shows the Widget 1 instead of Widget 2? – learncode Dec 28 '16 at 17:39