我做了这个代码..当我填写要求并按登录时应该打开一个新窗口但是当我这样做窗口打开然后它消失了..我看到很多代码使用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_()
在函数中创建的变量是本地的,因此它将在完成执行时被删除,并且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_())