我在pyqt中创建了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 Class,但是我的主文件太大了,我想在新的小部件中使用6-7个main方法,所以,我对如何处理这个问题有什么想法?
答案 0 :(得分:1)
在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