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?
In MainClass you must make the connection:
connectClass.pushButton.clicked.connect(self.funcfromMain)