Pyqt

by: Jowah, 6 years ago


thanks for all your free tutorials, i have been able to create a login page but i have a problem of closing the login dialog after successful login. please how do i close a dialog box automatically after successful login. thanks in advance

login.py

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

# Form implementation generated from reading ui file 'login.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
from admin import Ui_admin
import sqlite3

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainLogin(object):
     ################
    def login_check(self):
        username = self.leditUsername.text()
        password = self.leditPass.text()
        connection = sqlite3.connect("login.db")
        result = connection.execute("SELECT * FROM USERS WHERE USERNAME = ? AND PASSWORD = ?", (username, password))
        if(len(result.fetchall()) > 0):
            self.admin_window()
        else:
            self.inval_passwd('Warning','Invalid Username and Password combination')

    ### Login invalid password popup box
    def inval_passwd(self,title,message):
        msgBox = QtGui.QMessageBox()
        msgBox.setIcon(QtGui.QMessageBox.Warning)
        msgBox.setWindowTitle(title)
        msgBox.setText(message)
        msgBox.setStandardButtons(QtGui.QMessageBox.Ok)
        msgBox.exec_()
        
    ######Admin window popup
    def admin_window(self):
        self.apanel = QtGui.QMainWindow()
        self.ui = Ui_admin()
        self.ui.setupUi(self.apanel)
        self.apanel.show()
        
    ################
    def setupUi(self, MainLogin):
        MainLogin.setObjectName(_fromUtf8("MainLogin"))
        MainLogin.resize(400, 300)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("pythonlogo.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainLogin.setWindowIcon(icon)
        self.horizontalLayoutWidget_2 = QtGui.QWidget(MainLogin)
        self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(70, 130, 251, 41))
        self.horizontalLayoutWidget_2.setObjectName(_fromUtf8("horizontalLayoutWidget_2"))
        self.horizontalLayout_2 = QtGui.QHBoxLayout(self.horizontalLayoutWidget_2)
        self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
        self.label_2 = QtGui.QLabel(self.horizontalLayoutWidget_2)
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.label_2.setFont(font)
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.horizontalLayout_2.addWidget(self.label_2)
        spacerItem = QtGui.QSpacerItem(5, 0, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout_2.addItem(spacerItem)
        self.leditPass = QtGui.QLineEdit(self.horizontalLayoutWidget_2)
        self.leditPass.setMaxLength(25)
        self.leditPass.setObjectName(_fromUtf8("leditPass"))
        self.horizontalLayout_2.addWidget(self.leditPass)
        self.btnLogin = QtGui.QPushButton(MainLogin)
        self.btnLogin.setGeometry(QtCore.QRect(150, 230, 75, 31))
        self.btnLogin.setObjectName(_fromUtf8("btnLogin"))
        self.horizontalLayoutWidget = QtGui.QWidget(MainLogin)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(69, 89, 251, 41))
        self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
        self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.label = QtGui.QLabel(self.horizontalLayoutWidget)
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName(_fromUtf8("label"))
        self.horizontalLayout.addWidget(self.label)
        spacerItem1 = QtGui.QSpacerItem(10, 20, QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem1)
        self.leditUsername = QtGui.QLineEdit(self.horizontalLayoutWidget)
        self.leditUsername.setMaxLength(25)
        self.leditUsername.setObjectName(_fromUtf8("leditUsername"))
        self.horizontalLayout.addWidget(self.leditUsername)
        self.cmbSelect = QtGui.QComboBox(MainLogin)
        self.cmbSelect.setGeometry(QtCore.QRect(170, 180, 131, 22))
        self.cmbSelect.setObjectName(_fromUtf8("cmbSelect"))
        self.cmbSelect.addItem(_fromUtf8(""))
        self.cmbSelect.addItem(_fromUtf8(""))
        self.cmbSelect.addItem(_fromUtf8(""))
        self.label_3 = QtGui.QLabel(MainLogin)
        self.label_3.setGeometry(QtCore.QRect(140, 40, 121, 21))
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.label_3.setFont(font)
        self.label_3.setObjectName(_fromUtf8("label_3"))

        ##################
        self.btnLogin.clicked.connect(self.login_check)
        self.leditPass.setEchoMode(QtGui.QLineEdit.Password)
        ##################

        self.retranslateUi(MainLogin)
        QtCore.QMetaObject.connectSlotsByName(MainLogin)
        MainLogin.setTabOrder(self.leditUsername, self.leditPass)
        MainLogin.setTabOrder(self.leditPass, self.cmbSelect)
        MainLogin.setTabOrder(self.cmbSelect, self.btnLogin)

    def retranslateUi(self, MainLogin):
        MainLogin.setWindowTitle(_translate("MainLogin", "MainLogin", None))
        self.label_2.setText(_translate("MainLogin", "PASSWORD:", None))
        self.btnLogin.setText(_translate("MainLogin", "Login", None))
        self.label.setText(_translate("MainLogin", "USERNAME:", None))
        self.cmbSelect.setItemText(0, _translate("MainLogin", "Admin", None))
        self.cmbSelect.setItemText(1, _translate("MainLogin", "Nurse", None))
        self.cmbSelect.setItemText(2, _translate("MainLogin", "Doctor", None))
        self.label_3.setText(_translate("MainLogin", "ERP LOGIN", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainLogin = QtGui.QDialog()
    ui = Ui_MainLogin()
    ui.setupUi(MainLogin)
    MainLogin.show()
    sys.exit(app.exec_())



admin.py
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'admin.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
from patient_registration import *

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_admin(object):

    #######################
    def patience_RegPanel(self):
        self.patRegPanel = QtGui.QDialog()
        self.ui = Ui_patientRegistration()
        self.ui.setupUi(self.patRegPanel)
        self.patRegPanel.show()
        
    #######################
        
    def setupUi(self, admin):
        admin.setObjectName(_fromUtf8("admin"))
        admin.resize(703, 423)
        self.centralwidget = QtGui.QWidget(admin)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.btnPatientRegistration = QtGui.QPushButton(self.centralwidget)
        self.btnPatientRegistration.setGeometry(QtCore.QRect(130, 80, 141, 71))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.btnPatientRegistration.setFont(font)
        self.btnPatientRegistration.setObjectName(_fromUtf8("btnPatientRegistration"))
        self.btnRecord = QtGui.QPushButton(self.centralwidget)
        self.btnRecord.setGeometry(QtCore.QRect(130, 160, 141, 71))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.btnRecord.setFont(font)
        self.btnRecord.setObjectName(_fromUtf8("btnRecord"))
        self.btnStaffs = QtGui.QPushButton(self.centralwidget)
        self.btnStaffs.setGeometry(QtCore.QRect(320, 80, 141, 71))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.btnStaffs.setFont(font)
        self.btnStaffs.setObjectName(_fromUtf8("btnStaffs"))
        self.btnReport = QtGui.QPushButton(self.centralwidget)
        self.btnReport.setGeometry(QtCore.QRect(130, 240, 141, 71))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.btnReport.setFont(font)
        self.btnReport.setObjectName(_fromUtf8("btnReport"))
        self.btnTheater = QtGui.QPushButton(self.centralwidget)
        self.btnTheater.setGeometry(QtCore.QRect(320, 160, 141, 71))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.btnTheater.setFont(font)
        self.btnTheater.setObjectName(_fromUtf8("btnTheater"))
        self.btnPatientScheduling = QtGui.QPushButton(self.centralwidget)
        self.btnPatientScheduling.setGeometry(QtCore.QRect(320, 240, 141, 71))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.btnPatientScheduling.setFont(font)
        self.btnPatientScheduling.setObjectName(_fromUtf8("btnPatientScheduling"))
        self.btnWardManagment = QtGui.QPushButton(self.centralwidget)
        self.btnWardManagment.setGeometry(QtCore.QRect(510, 80, 131, 71))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.btnWardManagment.setFont(font)
        self.btnWardManagment.setObjectName(_fromUtf8("btnWardManagment"))
        self.btnBilling = QtGui.QPushButton(self.centralwidget)
        self.btnBilling.setGeometry(QtCore.QRect(510, 160, 131, 71))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.btnBilling.setFont(font)
        self.btnBilling.setObjectName(_fromUtf8("btnBilling"))
        self.lblAdminTime = QtGui.QLabel(self.centralwidget)
        self.lblAdminTime.setGeometry(QtCore.QRect(610, 20, 61, 21))
        self.lblAdminTime.setObjectName(_fromUtf8("lblAdminTime"))
        admin.setCentralWidget(self.centralwidget)

        #####################
        self.btnPatientRegistration.clicked.connect(self.patience_RegPanel)
        #####################

        self.retranslateUi(admin)
        QtCore.QMetaObject.connectSlotsByName(admin)
        admin.setTabOrder(self.btnPatientRegistration, self.btnRecord)
        admin.setTabOrder(self.btnRecord, self.btnReport)
        admin.setTabOrder(self.btnReport, self.btnStaffs)
        admin.setTabOrder(self.btnStaffs, self.btnTheater)
        admin.setTabOrder(self.btnTheater, self.btnPatientScheduling)
        admin.setTabOrder(self.btnPatientScheduling, self.btnWardManagment)
        admin.setTabOrder(self.btnWardManagment, self.btnBilling)

    def retranslateUi(self, admin):
        admin.setWindowTitle(_translate("admin", "Admin Panel", None))
        self.btnPatientRegistration.setText(_translate("admin", "Patience Registration", None))
        self.btnRecord.setText(_translate("admin", "Records", None))
        self.btnStaffs.setText(_translate("admin", "Staffs", None))
        self.btnReport.setText(_translate("admin", "Reports", None))
        self.btnTheater.setText(_translate("admin", "Theater", None))
        self.btnPatientScheduling.setText(_translate("admin", "Patient Scheduling", None))
        self.btnWardManagment.setText(_translate("admin", "Ward Managment", None))
        self.btnBilling.setText(_translate("admin", "Billing", None))
        self.lblAdminTime.setText(_translate("admin", "00:01:2018", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    admin = QtGui.QMainWindow()
    ui = Ui_admin()
    ui.setupUi(admin)
    admin.show()
    sys.exit(app.exec_())

db.py
import sqlite3

def createTable():
    connection = sqlite3.connect("login.db")
    connection.execute("CREATE TABLE USERS(USERNAME TEXT NOT NULL, PASSWORD TEXT)")
    connection.execute("INSERT INTO USERS VALUES(?,?)", ('lewis','lewis'))
    connection.commit()
    result = connection.execute("SELECT * FROM USERS")

    for data in result:
        print("Username: ", data[0])
        print("Password: ", data[1])

    connection.close()

createTable()        
    






You must be logged in to post. Please login or register an account.



add this line to the method to hide the main thread:

    def admin_window(self):
        self.apanel = QtGui.QMainWindow()
        self.ui = Ui_admin()
        self.ui.setupUi(self.apanel)
        self.apanel.show()
        MainLogin.hide()

and convert this:
str(username), str(password)

this also works MainLogin.close()

-HenryVC 6 years ago

You must be logged in to post. Please login or register an account.