我从我的主窗口中创建了2个新的小部件/窗口。现在,我想在MainWindow类中访问我的方法来进入我的新小部件。我怎样才能做到这一点?
这是我的代码:
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
来自相应的UI文件。
现在,我想将新UI文件中的按钮连接到MainClass中的方法,因此有关如何实现此目的的任何想法?
错误:(使用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'
更新1:我尝试使用mixin类,但是我的主文件很大,我想要在新小部件中像6-7个主要方法一样,所以我对如何处理的任何想法?
在主流中您必须建立连接: 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