简体   繁体   English

PySide打开一个新窗口,但立即关闭

[英]PySide open a new window, but close immediately

I have a question. 我有个问题。 Although some questions are similar with mine, but i cat't find how to resolve. 尽管有些问题与我的相似,但我找不到解决方法。 I create a login window, when click 'Confirm' button, hope to load a new window. 我创建一个登录窗口,当单击“确认”按钮时,希望加载一个新窗口。 But now the new window will show and close immediately.The code is below: 但是现在新窗口将立即显示并关闭,代码如下:

# -*- coding: utf-8 -*-

import sys
import time
from PySide.QtCore import *
from PySide.QtGui import *
import login
import moreFrame_modify

class show_login(QWidget,login.Ui_Form):
    def __init__(self,parent=None):
        super(show_login,self).__init__(parent)
        self.ui = login.Ui_Form()
        self.ui.setupUi(self)

    def show_notepad_clk(self):  #This function will trigger a new window
        print "Hello world!"
#         farm.destroy()
        time.sleep(1)
        showMemoUI().show()

    def exit_login_show(self):
        time.sleep(.1)
        exit()
#         self.destroy()

    def create_new_user(self):
        print "Create"
        QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')


class showMemoUI(QWidget,moreFrame_modify.Ui_Form):
    def __init__(self,parent=None):
        super(showMemoUI,self).__init__(parent)
        self.ui = moreFrame_modify.Ui_Form()
        print "Start show"
        self.ui.setupUi(self)
        print "Show notepad UI"


if __name__=='__main__':
    app = QApplication(sys.argv)
    farm = show_login()
    farm.show()
#     farm = showMemoUI()
#     farm.show()
    sys.exit(app.exec_())

login.py # - - coding: utf-8 - - login.py#-- 编码:utf- 8--

# Form implementation generated from reading ui file 'login.ui'
#
# Created: Mon Nov 12 17:25:34 2018
#      by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(291, 207)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/a15.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        Form.setWindowIcon(icon)
        self.verticalLayout_3 = QtGui.QVBoxLayout(Form)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.verticalLayout_2 = QtGui.QVBoxLayout()
        self.verticalLayout_2.setSpacing(20)
        self.verticalLayout_2.setContentsMargins(20, -1, 20, 20)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label = QtGui.QLabel(Form)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.userTxt = QtGui.QLineEdit(Form)
        self.userTxt.setObjectName("userTxt")
        self.horizontalLayout.addWidget(self.userTxt)
        self.verticalLayout_2.addLayout(self.horizontalLayout)
        self.horizontalLayout_2 = QtGui.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label_2 = QtGui.QLabel(Form)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout_2.addWidget(self.label_2)
        self.pwdTxt = QtGui.QLineEdit(Form)
        self.pwdTxt.setObjectName("pwdTxt")
        self.horizontalLayout_2.addWidget(self.pwdTxt)
        self.verticalLayout_2.addLayout(self.horizontalLayout_2)
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setSpacing(10)
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout_3 = QtGui.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.confirmBtn = QtGui.QPushButton(Form)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.confirmBtn.setFont(font)
        self.confirmBtn.setObjectName("confirmBtn")
        self.horizontalLayout_3.addWidget(self.confirmBtn)
        self.cancelBtn = QtGui.QPushButton(Form)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.cancelBtn.setFont(font)
        self.cancelBtn.setObjectName("cancelBtn")
        self.horizontalLayout_3.addWidget(self.cancelBtn)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.regeisterBtn = QtGui.QPushButton(Form)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.regeisterBtn.setFont(font)
        self.regeisterBtn.setObjectName("regeisterBtn")
        self.verticalLayout.addWidget(self.regeisterBtn)
        self.verticalLayout_2.addLayout(self.verticalLayout)
        self.verticalLayout_3.addLayout(self.verticalLayout_2)

        self.retranslateUi(Form)
        QtCore.QObject.connect(self.confirmBtn, QtCore.SIGNAL("clicked()"), Form.show_notepad_clk)
        QtCore.QObject.connect(self.cancelBtn, QtCore.SIGNAL("clicked()"), Form.exit_login_show)
        QtCore.QObject.connect(self.regeisterBtn, QtCore.SIGNAL("clicked()"), Form.create_new_user)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Login", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("Form", "UserName", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("Form", "Password", None, QtGui.QApplication.UnicodeUTF8))
        self.confirmBtn.setText(QtGui.QApplication.translate("Form", "Confirm", None, QtGui.QApplication.UnicodeUTF8))
        self.cancelBtn.setText(QtGui.QApplication.translate("Form", "Cancer", None, QtGui.QApplication.UnicodeUTF8))
        self.regeisterBtn.setText(QtGui.QApplication.translate("Form", "Add a new user", None, QtGui.QApplication.UnicodeUTF8))

import myresource_rc

moreFrame_modify.py moreFrame_modify.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'moreFrame_modify.ui'
#
# Created: Mon Nov 12 17:25:38 2018
#      by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(469, 458)
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Semibold")
        font.setWeight(75)
        font.setBold(True)
        Form.setFont(font)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/a11.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        Form.setWindowIcon(icon)
        self.horizontalLayout_10 = QtGui.QHBoxLayout(Form)
        self.horizontalLayout_10.setObjectName("horizontalLayout_10")
        self.horizontalLayout_9 = QtGui.QHBoxLayout()
        self.horizontalLayout_9.setObjectName("horizontalLayout_9")
        self.frame_3 = QtGui.QFrame(Form)
        self.frame_3.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame_3.setFrameShadow(QtGui.QFrame.Raised)
        self.frame_3.setObjectName("frame_3")
        self.horizontalLayout_4 = QtGui.QHBoxLayout(self.frame_3)
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout_2 = QtGui.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label = QtGui.QLabel(self.frame_3)
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Semibold")
        font.setPointSize(12)
        font.setWeight(75)
        font.setBold(True)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.horizontalLayout_2.addWidget(self.label)
        self.dateEdit = QtGui.QDateEdit(self.frame_3)
        self.dateEdit.setObjectName("dateEdit")
        self.horizontalLayout_2.addWidget(self.dateEdit)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_3 = QtGui.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.label_2 = QtGui.QLabel(self.frame_3)
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Semibold")
        font.setPointSize(12)
        font.setWeight(75)
        font.setBold(True)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout_3.addWidget(self.label_2)
        self.timeEdit = QtGui.QTimeEdit(self.frame_3)
        self.timeEdit.setObjectName("timeEdit")
        self.horizontalLayout_3.addWidget(self.timeEdit)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label_3 = QtGui.QLabel(self.frame_3)
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Semibold")
        font.setPointSize(12)
        font.setWeight(75)
        font.setBold(True)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.horizontalLayout.addWidget(self.label_3)
        self.addrTxt = QtGui.QLineEdit(self.frame_3)
        self.addrTxt.setObjectName("addrTxt")
        self.horizontalLayout.addWidget(self.addrTxt)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.horizontalLayout_5 = QtGui.QHBoxLayout()
        self.horizontalLayout_5.setObjectName("horizontalLayout_5")
        self.label_4 = QtGui.QLabel(self.frame_3)
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Semibold")
        font.setPointSize(12)
        font.setWeight(75)
        font.setBold(True)
        self.label_4.setFont(font)
        self.label_4.setObjectName("label_4")
        self.horizontalLayout_5.addWidget(self.label_4)
        self.commTxt = QtGui.QLineEdit(self.frame_3)
        self.commTxt.setObjectName("commTxt")
        self.horizontalLayout_5.addWidget(self.commTxt)
        self.verticalLayout.addLayout(self.horizontalLayout_5)
        self.textBrowser = QtGui.QTextBrowser(self.frame_3)
        self.textBrowser.setObjectName("textBrowser")
        self.verticalLayout.addWidget(self.textBrowser)
        self.horizontalLayout_4.addLayout(self.verticalLayout)
        self.verticalLayout_2 = QtGui.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.pushButton_4 = QtGui.QPushButton(self.frame_3)
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Semibold")
        font.setPointSize(12)
        font.setWeight(75)
        font.setBold(True)
        self.pushButton_4.setFont(font)
        self.pushButton_4.setObjectName("pushButton_4")
        self.verticalLayout_2.addWidget(self.pushButton_4)
        self.pushButton_5 = QtGui.QPushButton(self.frame_3)
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Semibold")
        font.setPointSize(12)
        font.setWeight(75)
        font.setBold(True)
        self.pushButton_5.setFont(font)
        self.pushButton_5.setObjectName("pushButton_5")
        self.verticalLayout_2.addWidget(self.pushButton_5)
        self.pushButton_6 = QtGui.QPushButton(self.frame_3)
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Semibold")
        font.setPointSize(12)
        font.setWeight(75)
        font.setBold(True)
        self.pushButton_6.setFont(font)
        self.pushButton_6.setObjectName("pushButton_6")
        self.verticalLayout_2.addWidget(self.pushButton_6)
        spacerItem = QtGui.QSpacerItem(20, 293, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.verticalLayout_2.addItem(spacerItem)
        self.horizontalLayout_4.addLayout(self.verticalLayout_2)
        self.horizontalLayout_9.addWidget(self.frame_3)
        self.horizontalLayout_10.addLayout(self.horizontalLayout_9)

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

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Notepad", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("Form", "Date", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("Form", "Time", None, QtGui.QApplication.UnicodeUTF8))
        self.label_3.setText(QtGui.QApplication.translate("Form", "Address", None, QtGui.QApplication.UnicodeUTF8))
        self.label_4.setText(QtGui.QApplication.translate("Form", "Comment", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_4.setText(QtGui.QApplication.translate("Form", "Save", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_5.setText(QtGui.QApplication.translate("Form", "Cancel", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_6.setText(QtGui.QApplication.translate("Form", "About", None, QtGui.QApplication.UnicodeUTF8))

import myresource_rc

Add login.py and moreFrame_Modify.py ,these two files are generated by QT designer. 添加login.py和moreFrame_Modify.py,这两个文件由QT设计器生成。

In your case you have 2 errors: 就您而言,您有2个错误:

  • You should not use time.sleep() in a GUI, in the case of Qt you should use QTimer since time.sleep() freezes the GUI. 您不应该在GUI中使用time.sleep() ,对于Qt,您应该使用QTimer因为time.sleep()冻结GUI。

  • A local variable created in a function only exists as long as the function is executed, in your case showMemoUI is shown and instantly eliminated, therefore the new window is not observed. 在函数中创建的局部变量仅在函数执行后才存在,在您的情况下, showMemoUI会显示并立即删除,因此不会观察到新窗口。 The solution is to make the new window member of the class. 解决方案是使新窗口成为该类的成员。

Using the above, the solution code is: 使用以上方法,解决方案代码为:

import sys
from PySide import QtCore, QtGui
import login
import moreFrame_modify

class show_login(QtGui.QWidget, login.Ui_Form):
    def __init__(self,parent=None):
        super(show_login,self).__init__(parent)
        self.setupUi(self)
        self.showmemoui = showMemoUI()

    def show_notepad_clk(self):  #This function will trigger a new window
        print("Hello world!")
        QtCore.QTimer.singleShot(1000, self.showmemoui.show)

    def exit_login_show(self):
        QtCore.QTimer.singleShot(100, QtGui.QApplication.quit)

    def create_new_user(self):
        print("Create")
        QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')

class showMemoUI(QtGui.QWidget, moreFrame_modify.Ui_Form):
    def __init__(self, parent=None):
        super(showMemoUI,self).__init__(parent)
        print("Start show")
        self.setupUi(self)
        print("Show notepad UI")

if __name__=='__main__':
    app = QtGui.QApplication(sys.argv)
    farm = show_login()
    farm.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM