从New
单击动作MenuBar
时,我试图启动新对话框,但是当我单击该动作时,程序崩溃了,并且没有打开新窗口。
下面是我从QT Designer中提取的代码,然后根据需要进行了更改。
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import QtCore, QtWidgets
import sys
class main_window(QtWidgets.QMainWindow):
switch_window = QtCore.pyqtSignal(str)
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
QtWidgets.QWidget.__init__(self)
self.setObjectName("mainWindow")
self.resize(792, 600)
self.centralwidget = QtWidgets.QMainWindow(self)
self.centralwidget.setObjectName("centralwidget")
self.splitter = QtWidgets.QSplitter(self.centralwidget)
self.splitter.setGeometry(QtCore.QRect(20, 80, 180, 20))
self.splitter.setOrientation(QtCore.Qt.Horizontal)
self.splitter.setObjectName("splitter")
self.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(self)
self.menubar.setGeometry(QtCore.QRect(0, 0, 792, 21))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuView = QtWidgets.QMenu(self.menubar)
self.menuView.setObjectName("menuView")
self.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(self)
self.statusbar.setObjectName("statusbar")
self.setStatusBar(self.statusbar)
self.actionNew = QtWidgets.QAction(self)
self.actionNew.setObjectName("actionNew")
self.menuFile.addSeparator()
self.menuFile.addAction(self.actionNew)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuView.menuAction())
self.setWindowTitle("MainWindow")
self.menuFile.setTitle("File")
self.menuView.setTitle("View")
self.actionNew.setText("LandForm")
self.actionNew.triggered.connect(self.switch)
def switch(self):
self.switch_window.emit()
class land_form(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setObjectName("LandForm")
self.resize(660, 482)
self.gridLayout = QtWidgets.QGridLayout(self)
self.gridLayout.setObjectName("gridLayout")
self.LandFormData = QtWidgets.QListView(self)
self.LandFormData.setObjectName("LandFormData")
self.gridLayout.addWidget(self.LandFormData, 0, 0, 1, 5)
self.setWindowTitle("Dialog")
class MyForm:
def __init__(self):
pass
def main_window(self):
self.main_window_create = main_window()
self.main_window_create.switch_window.connect(self.land_form)
self.main_window_create.show()
def land_form(self):
self.land_form_create = land_form()
self.land_form_create.show()
def main():
main_app = QtWidgets.QApplication(sys.argv)
main_my_app = MyForm()
main_my_app.main_window()
sys.exit(main_app.exec_())
if __name__ == "__main__":
main()