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

时间:2018-11-13 02:37:28

标签: python pyside

我有一个问题。尽管有些问题与我的相似,但我找不到解决方法。 我创建一个登录窗口,当单击“确认”按钮时,希望加载一个新窗口。但是现在新窗口将立即显示并关闭。代码如下:

# -*- 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     #--编码: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

# -*- 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

添加login.py和moreFrame_Modify.py,这两个文件由QT设计器生成。

1 个答案:

答案 0 :(得分:1)

在您的情况下,您有2个错误:

  • 您不应在GUI中使用$.ajax({ dataType: 'JSON', type: 'POST', url: "today_attendance.php", success: function(response) { Morris.Bar({ element: 'morris-bar', data: response, xkey: 'x', ykeys: ['y', 'z'], labels: ['A', 'B'] }); } }); ,对于Qt,您应使用time.sleep(),因为QTimer会冻结GUI。

  • 在函数中创建的局部变量仅在函数执行时才存在,在您的情况下,time.sleep()会显示并立即消除,因此不会观察到新窗口。解决方案是使类的新窗口成员。

使用上述方法,解决方案代码为:

showMemoUI