按下按钮时在PyQt中在屏幕之间移动

时间:2019-09-07 10:39:36

标签: python pyqt pyqt5

我正在构建用于PyQt5练习的餐厅应用程序,我有2个问题: 我有一个主屏幕,它将把用户带到其他屏幕

  1. 第二次在屏幕之间切换时出现错误,它崩溃了
  2. 当我移至新窗口时,以前的窗口不会关闭,因此每次都是新窗口时

我尝试搜索有关此信息,而我所能找到的就是如何在同一屏幕上而不是在新屏幕上进行操作

供您查看的代码:

第一个屏幕:

from mainMenu import*
from PyQt5 import QtCore, QtGui, QtWidgets    
mod=0#0 - TA , 1- Delivery
class Ui_Main():
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1072, 747)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.ta = QtWidgets.QPushButton(self.centralwidget)
        self.ta.setGeometry(QtCore.QRect(680, 50, 191, 171))
        self.ta.setObjectName("ta")
        self.ta.clicked.connect(self.taClicked)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1072, 22))
        self.menubar.setObjectName("menubar")
        self.menuMain = QtWidgets.QMenu(self.menubar)
        self.menuMain.setObjectName("menuMain")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.menubar.addAction(self.menuMain.menuAction())
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", 
       "MainWindow"))

        self.ta.setText(_translate("MainWindow", "TA"))
        self.menuMain.setTitle(_translate("MainWindow", "Main"))

    def taClicked(self):
        mod=0
        print("here")
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_MainMenu()  # ------------->creating an object
        self.ui.setupUi(self.window)
        self.window.show()


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_Main()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

第二个屏幕:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from main import*

class Ui_MainMenu(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1073, 738)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
        self.graphicsView.setGeometry(QtCore.QRect(685, 40, 331, 251))
        self.graphicsView.setObjectName("graphicsView")
        self.line = QtWidgets.QFrame(self.centralwidget)
        self.line.setGeometry(QtCore.QRect(80, 330, 561, 20))
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(190, 385, 71, 41))
        self.label.setObjectName("label")
        self.pushButton_14 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_14.setGeometry(QtCore.QRect(190, 440, 81, 51))
        self.pushButton_14.setObjectName("pushButton_14")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(800, 380, 81, 16))
        self.label_2.setObjectName("label_2")
        self.spinBox = QtWidgets.QSpinBox(self.centralwidget)
        self.spinBox.setGeometry(QtCore.QRect(727, 370, 61, 41))
        self.spinBox.setObjectName("spinBox")

        self.pushButton_40 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_40.setGeometry(QtCore.QRect(750, 600, 141, 41))
        self.pushButton_40.setObjectName("pushButton_40")
        self.pushButton_40.clicked.connect(self.btnclicked)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1073, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainMenu", "MainMenu"))

        self.pushButton_40.setText(_translate("MainWindow", "Cancel"))

    def btnclicked(self):
        print("here")
        self.window = QtWidgets.QMainWindow()
        self.m= Ui_Main()
        self.m.setupUi(self.window)
        self.window.show()



if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainMenu()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

错误:

Traceback (most recent call last):
  File "/Users/adir/PycharmProjects/main.py", line 61, in taClicked
    self.ui = Ui_MainMenu()  # ------------->creating an object
NameError: name 'Ui_MainMenu' is not defined

1 个答案:

答案 0 :(得分:0)

尝试一下:

#from mainMenu import*
from PyQt5 import QtCore, QtGui, QtWidgets   

mod = 0    #0 - TA , 1- Delivery


class Ui_MainMenu(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1073, 738)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
        self.graphicsView.setGeometry(QtCore.QRect(685, 40, 331, 251))
        self.graphicsView.setObjectName("graphicsView")
        self.line = QtWidgets.QFrame(self.centralwidget)
        self.line.setGeometry(QtCore.QRect(80, 330, 561, 20))
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(190, 385, 71, 41))
        self.label.setObjectName("label")
        self.pushButton_14 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_14.setGeometry(QtCore.QRect(190, 440, 81, 51))
        self.pushButton_14.setObjectName("pushButton_14")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(800, 380, 81, 16))
        self.label_2.setObjectName("label_2")
        self.spinBox = QtWidgets.QSpinBox(self.centralwidget)
        self.spinBox.setGeometry(QtCore.QRect(727, 370, 61, 41))
        self.spinBox.setObjectName("spinBox")

        self.pushButton_40 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_40.setGeometry(QtCore.QRect(750, 600, 141, 41))
        self.pushButton_40.setObjectName("pushButton_40")
#        self.pushButton_40.clicked.connect(self.btnclicked)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1073, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainMenu", "MainMenu"))

        self.pushButton_40.setText(_translate("MainWindow", "Cancel"))


class Ui_Main():
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1072, 747)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.ta = QtWidgets.QPushButton(self.centralwidget)
        self.ta.setGeometry(QtCore.QRect(680, 50, 191, 171))
        self.ta.setObjectName("ta")
#        self.ta.clicked.connect(self.taClicked)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1072, 22))
        self.menubar.setObjectName("menubar")
        self.menuMain = QtWidgets.QMenu(self.menubar)
        self.menuMain.setObjectName("menuMain")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.menubar.addAction(self.menuMain.menuAction())
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", 
       "MainWindow"))

        self.ta.setText(_translate("MainWindow", "TA"))
        self.menuMain.setTitle(_translate("MainWindow", "Main"))


class MainWindow(QtWidgets.QMainWindow, Ui_Main):
    def __init__(self):
        super().__init__()

        self.setupUi(self)
        self.ta.clicked.connect(self.taClicked)

    def taClicked(self):
        mod = 0
        print("here")

        self.window = QtWidgets.QMainWindow()
        self.mainMenu = Ui_MainMenu()                                # ------------->creating an object
        self.mainMenu.setupUi(self.window)
        self.mainMenu.pushButton_40.clicked.connect(self.btnclicked)

        self.window.show()
        self.hide()

    def btnclicked(self):
        self.window.hide()
        self.show()        

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
#    MainWindow = QtWidgets.QMainWindow()
#    ui = Ui_Main()
#    ui.setupUi(MainWindow)
#    MainWindow.show()
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())