简体   繁体   English

无法使用PyQt4打开新窗口

[英]Can't open a new window with PyQt4

I made this code .. A new window should open when i fill the requirements and press login but when i do that the window open then it disappears .. i saw a lot of codes using OOP but i don't understand them so i need any one to give me a simple solution 我做了这段代码..当我满足要求并按登录时,应该会打开一个新窗口,但是当我打开该窗口时,它就会消失..我看到了很多使用OOP的代码,但我不理解它们,所以我需要任何人给我一个简单的解决方案

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import os
import shutil
app = QApplication(sys.argv)
main_window = QWidget()
main_window.setWindowTitle("Keep It Safe V1.5")
main_window.setWindowIcon(QIcon('lock.png'))
main_window.resize(350, 180)
main_window.move(500, 200)
login_btn = QPushButton('Login', main_window)
login_btn.resize(150, 30)
login_btn.move(100, 120)
User = QLineEdit(main_window)
User.resize(250, 30)
User.move(50, 10)
User.setPlaceholderText('Enter your user name')
password = QLineEdit(main_window)
password.resize(250, 30)
password.move(50, 60)
password.setPlaceholderText('Enter your passsword')
password.setEchoMode(QLineEdit.Password)
check = QCheckBox(main_window, text="I accept the terms and policies")
check.move(50, 95)
def login_check():
    user = User.text()
    Pass = password.text()
    if user == "Admin" and Pass == "admin" and check.isChecked():
        print("Clicked")
        sec_win =QWidget()
        l = QLabel(sec_win , text = "second window opened")
        sec_win.show()
    else:
        fai = QMessageBox.warning(main_window, "Error", "Incorrect user name or passwprd")
login_btn.clicked.connect(login_check)
main_window.show()
app.exec_()

A variable that is created within a function is local, so it will be deleted when it is finished executing, and sec_win is so it will be displayed but an instant later it will be removed, the solution is to create it before and only show it when necessary. 在函数内创建的变量是局部变量,因此在执行完毕后将被删除,而sec_win则将被显示,但稍后将被删除,解决方案是在之前创建它并仅显示它必要时。

import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *

app = QApplication(sys.argv)
main_window = QWidget()
main_window.setWindowTitle("Keep It Safe V1.5")
main_window.setWindowIcon(QIcon('lock.png'))
main_window.resize(350, 180)
main_window.move(500, 200)
login_btn = QPushButton('Login', main_window)
login_btn.resize(150, 30)
login_btn.move(100, 120)
User = QLineEdit(main_window)
User.resize(250, 30)
User.move(50, 10)
User.setPlaceholderText('Enter your user name')
password = QLineEdit(main_window)
password.resize(250, 30)
password.move(50, 60)
password.setPlaceholderText('Enter your passsword')
password.setEchoMode(QLineEdit.Password)
check = QCheckBox(main_window, text="I accept the terms and policies")
check.move(50, 95)

sec_win =QWidget()

def login_check():
    user = User.text()
    Pass = password.text()
    if user == "Admin" and Pass == "admin" and check.isChecked():
        print("Clicked")
        l = QLabel(sec_win , text = "second window opened")
        sec_win.show()
    else:
        fai = QMessageBox.warning(main_window, "Error", "Incorrect user name or passwprd")

login_btn.clicked.connect(login_check)
main_window.show()
sys.exit(app.exec_())

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

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