简体   繁体   English

来自另一个类PyQt4的访问方法

[英]Access methods from another class PyQt4

I have created 2 new widgets/windows in pyqt, from my my main window. 我从主窗口在pyqt中创建了2个新的小部件/窗口。 Now I would like to access my methods in mainwindow class to my new widgets. 现在,我想在mainwindow类中的方法访问新的小部件。 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. self.pushButton_2 and self.pushButton来自相应的UI文件。

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? 现在,我想将New UI文件中的此类按钮连接到MainClass中的方法,那么关于如何实现此目的的任何想法吗?

Error: (with parent.func ) 错误:(带有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? 更新1:我尝试使用Mixin类,但是我的主文件太大,我想在新的小部件中使用6-7个main方法,因此,关于如何实现此目的的任何想法吗?

In MainClass you must make the connection: connectClass.pushButton.clicked.connect(self.funcfromMain) 在MainClass中,您必须建立连接: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM