第二个窗口消失并且没有进入循环

时间:2019-07-05 13:52:58

标签: python-3.x pyqt5

我在pyqt5中有一些小代码,用于显示第一个窗口,单击按钮后显示第二个窗口,对窗口小部件进行了一些操作,但是除一件事情外其他所有东西都起作用,最后第二个窗口消失了 T

我尝试对代码进行一些修改,还在此处检查了一些帖子,但没有任何效果

import sys

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QLineEdit

from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget, QTableWidgetItem, QVBoxLayout
from qtpy import QtWidgets

app = QtWidgets.QApplication(sys.argv)
MainWind = QtWidgets.QMainWindow()


class Ui_LoginWindow(object):
    def setupUi(self, LoginWindow):
        LoginWindow.setObjectName("LoginWindow")
        LoginWindow.resize(418, 508)
        self.LoginWidget = QtWidgets.QWidget(LoginWindow)
        self.LoginWidget.setObjectName("LoginWidget")
        self.User_Input = QtWidgets.QLineEdit(self.LoginWidget)
        self.User_Input.setGeometry(QtCore.QRect(100, 100, 201, 41))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.User_Input.setFont(font)
        self.User_Input.setObjectName("User_Input")
        self.Password_Input = QtWidgets.QLineEdit(self.LoginWidget)
        self.Password_Input.setGeometry(QtCore.QRect(100, 220, 201, 41))
        self.Password_Input.setEchoMode(QLineEdit.Password)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setStrikeOut(False)
        self.Password_Input.setFont(font)
        self.Password_Input.setInputMask("")
        self.Password_Input.setText("")
        self.Password_Input.setObjectName("Password_Input")
        self.User_Label = QtWidgets.QLabel(self.LoginWidget)
        self.User_Label.setGeometry(QtCore.QRect(140, 70, 125, 21))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.User_Label.setFont(font)
        self.User_Label.setAlignment(QtCore.Qt.AlignCenter)
        self.User_Label.setObjectName("User_Label")
        self.Password_Label = QtWidgets.QLabel(self.LoginWidget)
        self.Password_Label.setEnabled(True)
        self.Password_Label.setGeometry(QtCore.QRect(160, 190, 91, 21))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.Password_Label.setFont(font)
        self.Password_Label.setScaledContents(False)
        self.Password_Label.setAlignment(QtCore.Qt.AlignCenter)
        self.Password_Label.setObjectName("Password_Label")
        self.Login_Button = QtWidgets.QPushButton(self.LoginWidget)
        self.Login_Button.setGeometry(QtCore.QRect(120, 310, 165, 29))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.Login_Button.setFont(font)
        self.Login_Button.setObjectName("Login_Button")
        self.Message_Label = QtWidgets.QLabel(self.LoginWidget)
        self.Message_Label.setGeometry(QtCore.QRect(20, 420, 371, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setItalic(True)
        self.Message_Label.setFont(font)
        self.Message_Label.setAlignment(QtCore.Qt.AlignCenter)
        self.Message_Label.setWordWrap(True)
        self.Message_Label.setObjectName("Message_Label")
        LoginWindow.setCentralWidget(self.LoginWidget)

        self.retranslateUi(LoginWindow)
        QtCore.QMetaObject.connectSlotsByName(LoginWindow)

    def retranslateUi(self, LoginWindow):
        _translate = QtCore.QCoreApplication.translate
        LoginWindow.setWindowTitle(_translate("LoginWindow", "მთავარი გვერდი"))
        self.User_Label.setText(_translate("LoginWindow", "მომხმარებელი"))
        self.Password_Label.setText(_translate("LoginWindow", "პაროლი"))
        self.Login_Button.setText(_translate("LoginWindow", "სისტემაში შესვლა"))
        self.Message_Label.setText(_translate("LoginWindow", "<html><head/><body><p><br/></p></body></html>"))
        self.Login_Button.clicked.connect(self.log_click)

    def log_click(self):

        MainWind.hide()
        Second()

################################################
#This is where it starts does some work and disappears at the end
class Second(QWidget):
    def __init__(self, parent=None):
        super(Second, self).__init__(parent)
        self.setWindowTitle('შეიყვანე ჩვენება')
        self.setGeometry(0, 0, 640, 480)

        self.tableWidget = QTableWidget()

        grid_height = 4
        grid_width = 3

        self.tableWidget.setRowCount(grid_height)
        self.tableWidget.setColumnCount(grid_width)

        for x in range(0, grid_height):
            for y in range(0, grid_width):

                self.tableWidget.setItem(x, y, QTableWidgetItem('test1'))
                self.tableWidget.setEditTriggers(QtWidgets.QTableWidget.NoEditTriggers)

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.tableWidget)
        self.setLayout(self.layout)

        self.show()


def Login_Page():
    ui = Ui_LoginWindow()
    ui.setupUi(MainWind)
    MainWind.show()
    sys.exit(app.exec_())


def main():
    Login_Page()


if __name__ == "__main__":
    main()

只希望第二个窗口保持静止

1 个答案:

答案 0 :(得分:1)

只需替换此行:

Second()

通过这个:

self.second = Second()

这是一个常见的问题,python(其垃圾收集器)在创建新窗口后会立即删除它,因为没有更多的引用了。将其存储为类属性可以解决此问题。

此外,将show()方法移到__init__类的Second之外也是一种好习惯,因为您可能想实例化Second()对象显示它,例如用于测试。