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
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.
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_())