PYQT4:使用Windows用户名和密码进行桌面应用程序

时间:2017-03-01 19:48:21

标签: python windows login pyqt passwords

目前,我正在使用SQLite数据库让用户登录我的GUI应用程序。我使用以下代码:

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

# Form implementation generated from reading ui file 'InfectionControlLogin.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
import sqlite3
import LoginIC_rc
import sys
import time
import os.path as osp
import ctypes
from ctypes import wintypes
import os


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)


#setting the path variable for icon
path = osp.join(osp.dirname(sys.modules[__name__].__file__), 'I:\\Infection Control System Database\images\Malware-icon.png')
myappid = 'I:\\Infection Control System Database\images\Malware-icon.png' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
lpBuffer = wintypes.LPWSTR()
AppUserModelID = ctypes.windll.shell32.GetCurrentProcessExplicitAppUserModelID
AppUserModelID(ctypes.cast(ctypes.byref(lpBuffer), wintypes.LPWSTR))
appid = lpBuffer.value
ctypes.windll.kernel32.LocalFree(lpBuffer)
if appid is not None:
    print(appid)
class Ui_Dialog(object):

    def signUpShow(self):
        self.signUpWindow = QtGui.QDialog()
        self.ui = Ui_signUp()
        self.ui.setupUi(self.signUpWindow)
        self.signUpWindow.show()

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setWindowFlags(QtCore.Qt.WindowCloseButtonHint)
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(443, 383)
        Dialog.setMinimumSize(QtCore.QSize(443, 383))
        Dialog.setMaximumSize(QtCore.QSize(443, 383))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("I:\Infection Control System Database\Program Files\images\ICDatabaseImage.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        Dialog.setWindowIcon(icon)
        Dialog.setStyleSheet(_fromUtf8(""))
        Dialog.setModal(False)
        self.groupLogin = QtGui.QGroupBox(Dialog)
        self.groupLogin.setGeometry(QtCore.QRect(210, 180, 221, 171))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Times New Roman"))
        font.setPointSize(14)
        self.groupLogin.setFont(font)
        self.groupLogin.setStyleSheet(_fromUtf8("color: rgb(4, 4, 4);\n"
"alternate-background-color: rgb(255, 255, 255);"))
        self.groupLogin.setAlignment(QtCore.Qt.AlignCenter)
        self.groupLogin.setFlat(False)
        self.groupLogin.setObjectName(_fromUtf8("groupLogin"))
        self.btnLogin = QtGui.QPushButton(self.groupLogin)
        self.btnLogin.setGeometry(QtCore.QRect(20, 130, 81, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.btnLogin.setFont(font)
        self.btnLogin.setStatusTip(_fromUtf8(""))
        self.btnLogin.setWhatsThis(_fromUtf8(""))
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8(":/icimages/Check.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btnLogin.setIcon(icon1)
        self.btnLogin.setIconSize(QtCore.QSize(20, 20))
        self.btnLogin.setAutoDefault(True)
        self.btnLogin.setDefault(True)
        self.btnLogin.setObjectName(_fromUtf8("btnLogin"))
        self.lineEdit_Password = QtGui.QLineEdit(self.groupLogin)
        self.lineEdit_Password.setGeometry(QtCore.QRect(10, 90, 201, 31))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Times New Roman"))
        font.setPointSize(10)
        self.lineEdit_Password.setFont(font)
        self.lineEdit_Password.setStatusTip(_fromUtf8(""))
        self.lineEdit_Password.setMaxLength(25)
        self.lineEdit_Password.setEchoMode(QtGui.QLineEdit.Password)
        self.lineEdit_Password.setProperty("clearButtonEnabled", True)
        self.lineEdit_Password.setObjectName(_fromUtf8("lineEdit_Password"))
        self.lineEdit_Username = QtGui.QLineEdit(self.groupLogin)
        self.lineEdit_Username.setGeometry(QtCore.QRect(10, 40, 201, 31))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Times New Roman"))
        font.setPointSize(10)
        self.lineEdit_Username.setFont(font)
        self.lineEdit_Username.setStatusTip(_fromUtf8(""))
        self.lineEdit_Username.setWhatsThis(_fromUtf8(""))
        #self.lineEdit_Username.setAccessibleName(_fromUtf8(""))
        self.lineEdit_Username.setMaxLength(20)
        self.lineEdit_Username.setProperty("clearButtonEnabled", True)
        self.lineEdit_Username.setObjectName(_fromUtf8("lineEdit_Username"))
        self.btnCancel = QtGui.QPushButton(self.groupLogin)
        self.btnCancel.setGeometry(QtCore.QRect(120, 130, 81, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(False)
        font.setWeight(50)
        font.setStrikeOut(False)
        font.setKerning(True)
        self.btnCancel.setFont(font)
        self.btnCancel.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.btnCancel.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.btnCancel.setAutoFillBackground(True)
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(_fromUtf8(":/icimages/Cancel.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btnCancel.setIcon(icon2)
        self.btnCancel.setIconSize(QtCore.QSize(15, 15))
        self.btnCancel.setFlat(False)
        self.btnCancel.setObjectName(_fromUtf8("btnCancel"))
        self.lblUsername = QtGui.QLabel(self.groupLogin)
        self.lblUsername.setGeometry(QtCore.QRect(10, 20, 61, 16))
        font = QtGui.QFont()
        font.setUnderline(True)
        self.lblUsername.setFont(font)
        self.lblUsername.setObjectName(_fromUtf8("lblUsername"))
        self.lblPassword = QtGui.QLabel(self.groupLogin)
        self.lblPassword.setGeometry(QtCore.QRect(10, 70, 61, 16))
        font = QtGui.QFont()
        font.setUnderline(True)
        self.lblPassword.setFont(font)
        self.lblPassword.setObjectName(_fromUtf8("lblPassword"))
        self.toolButton = QtGui.QToolButton(self.groupLogin)
        self.toolButton.setGeometry(QtCore.QRect(182, 91, 28, 28))
        self.toolButton.setAutoFillBackground(False)
        self.toolButton.setText(_fromUtf8(""))
        icon3 = QtGui.QIcon()
        icon3.addPixmap(QtGui.QPixmap(_fromUtf8(":/icimages/Lock.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.toolButton.setIcon(icon3)
        self.toolButton.setIconSize(QtCore.QSize(40, 40))
        self.toolButton.setCheckable(False)
        self.toolButton.setPopupMode(QtGui.QToolButton.InstantPopup)
        self.toolButton.setAutoRaise(True)
        self.toolButton.setObjectName(_fromUtf8("toolButton"))
        self.lblConnectionStatus = QtGui.QLabel(Dialog)
        self.lblConnectionStatus.setGeometry(QtCore.QRect(140, 360, 131, 21))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.lblConnectionStatus.setFont(font)
        self.lblConnectionStatus.setStatusTip(_fromUtf8(""))
        self.lblConnectionStatus.setWhatsThis(_fromUtf8(""))
        self.lblConnectionStatus.setStyleSheet(_fromUtf8("background-color: rgb(226, 226, 226);"))
        self.lblConnectionStatus.setFrameShape(QtGui.QFrame.StyledPanel)
        self.lblConnectionStatus.setObjectName(_fromUtf8("lblConnectionStatus"))
        self.lblWelcome = QtGui.QLabel(Dialog)
        self.lblWelcome.setGeometry(QtCore.QRect(210, 120, 231, 41))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Microsoft Uighur"))
        font.setPointSize(40)
        self.lblWelcome.setFont(font)
        self.lblWelcome.setFrameShape(QtGui.QFrame.NoFrame)
        self.lblWelcome.setFrameShadow(QtGui.QFrame.Raised)
        self.lblWelcome.setAlignment(QtCore.Qt.AlignCenter)
        self.lblWelcome.setObjectName(_fromUtf8("lblWelcome"))
        self.lblResult = QtGui.QLabel(Dialog)
        self.lblResult.setGeometry(QtCore.QRect(280, 360, 151, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setItalic(True)
        self.lblResult.setFont(font)
        self.lblResult.setFrameShape(QtGui.QFrame.StyledPanel)
        self.lblResult.setFrameShadow(QtGui.QFrame.Raised)
        self.lblResult.setScaledContents(True)
        self.lblResult.setAlignment(QtCore.Qt.AlignCenter)
        self.lblResult.setObjectName(_fromUtf8("lblResult"))
        self.lbl_InfectionControlApp = QtGui.QLabel(Dialog)
        self.lbl_InfectionControlApp.setGeometry(QtCore.QRect(190, 150, 271, 31))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Script MT Bold"))
        font.setBold(True)
        font.setWeight(75)
        self.lbl_InfectionControlApp.setFont(font)
        self.lbl_InfectionControlApp.setFrameShape(QtGui.QFrame.NoFrame)
        self.lbl_InfectionControlApp.setFrameShadow(QtGui.QFrame.Raised)
        self.lbl_InfectionControlApp.setObjectName(_fromUtf8("lbl_InfectionControlApp"))
        self.frame = QtGui.QFrame(Dialog)
        self.frame.setGeometry(QtCore.QRect(-50, -10, 1101, 601))
        self.frame.setStyleSheet(_fromUtf8("border-image: url(:/icimages/InfectionControlApp.png);"))
        self.frame.setFrameShape(QtGui.QFrame.NoFrame)
        self.frame.setFrameShadow(QtGui.QFrame.Raised)
        self.frame.setLineWidth(0)
        self.frame.setObjectName(_fromUtf8("frame"))
        self.fram_INOVA = QtGui.QFrame(self.frame)
        self.fram_INOVA.setGeometry(QtCore.QRect(340, 40, 81, 71))
        self.fram_INOVA.setStyleSheet(_fromUtf8("border-image: url(:/icimages/INOVA.png);"))
        self.fram_INOVA.setFrameShape(QtGui.QFrame.NoFrame)
        self.fram_INOVA.setFrameShadow(QtGui.QFrame.Plain)
        self.fram_INOVA.setObjectName(_fromUtf8("fram_INOVA"))
        self.frame.raise_()
        self.groupLogin.raise_()
        self.lblConnectionStatus.raise_()
        self.lblWelcome.raise_()
        self.lblResult.raise_()
        self.lbl_InfectionControlApp.raise_()

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        Dialog.setTabOrder(self.lineEdit_Username, self.lineEdit_Password)
        Dialog.setTabOrder(self.lineEdit_Password, self.toolButton)
        Dialog.setTabOrder(self.toolButton, self.btnLogin)
        Dialog.setTabOrder(self.btnLogin, self.btnCancel)

        ######################### Button Event ##############################
        self.btnLogin.clicked.connect(self.loginCheck)
        #####################################################################
        ######################### Button Event ##############################
        self.btnCancel.clicked.connect(self.fieldsclear)
        #####################################################################
        ######################### Button Event ##############################
        self.toolButton.pressed.connect(self.text_show)
        #####################################################################
        ######################### Text Event Change #########################
        self.lineEdit_Username.textChanged.connect(self.text_changed)
        #####################################################################
        ######################### Password Text Event Change ################
        self.toolButton.released.connect(self.text_hide)
        #####################################################################
        ######################### Text Event Change #########################
        self.lineEdit_Password.textChanged.connect(self.text_changed_Password)
        #####################################################################

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Infection Control Database - Login Page", None))
        self.groupLogin.setTitle(_translate("Dialog", "Login", None))
        self.btnLogin.setText(_translate("Dialog", "Login", None))
        self.btnCancel.setText(_translate("Dialog", "Cancel", None))
        self.lblUsername.setText(_translate("Dialog", "Username:", None))
        self.lblPassword.setText(_translate("Dialog", "Password:", None))
        self.lblConnectionStatus.setText(_translate("Dialog", "<html><head/><body><p align=\"center\"><span style=\" font-size:10pt; color:#aa0000;\">Connection Status:</span></p></body></html>", None))
        self.lblWelcome.setText(_translate("Dialog", "Welcome!", None))
        self.lblResult.setText(_translate("Dialog", "<html><head/><body><p align=\"center\">Status</p></body></html>", None))
        self.lbl_InfectionControlApp.setText(_translate("Dialog", "<html><head/><body><p align=\"center\"><span style=\" font-size:12pt; font-weight:600; color:#aa0000;\">Infection Control Application</span></p></body></html>", None))

        if self.lineEdit_Username.text() == "":
           self.lblResult.setText("Establishing Connection")
        if self.lineEdit_Password.text() == "":
            self.btnLogin.setEnabled(False)

    def fieldsclear(self):
        self.lineEdit_Username.setText("")
        self.lineEdit_Password.setText("")

    def text_show(self):
        self.lineEdit_Password.setEchoMode(QtGui.QLineEdit.Normal)



    def loginCheck(self):
        import os
        username = self.lineEdit_Username.text()
        password = self.lineEdit_Password.text()

        connection = sqlite3.connect("login.db")
        result = connection.execute("SELECT * FROM USERS WHERE USERNAME = ? AND PASSWORD = ?",(username,password))
        if(len(result.fetchall()) > 0):
            os.startfile('I:\\Infection Control System Database\Program Files\MainWindow\icMainWin_init.py')
            Dialog.close()

        else:
            print("User Not Found !")
            self.showMessageBox('Warning','Invalid Username and/or Password! Please Try Again.')
            self.lineEdit_Username.setText("")
            self.lineEdit_Password.setText("")
        connection.close()
    def showMessageBox(self,title,message):
        msgBox = QtGui.QMessageBox()
        msgBox.setIcon(QtGui.QMessageBox.Warning)
        msgBox.setWindowTitle(title)
        msgBox.setText(message)
        msgBox.setStandardButtons(QtGui.QMessageBox.Ok)
        msgBox.exec_()

    def welcomeWindowShow(self):
        self.welcomeWindow = QtGui.QMainWindow()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self.welcomeWindow)
        self.welcomeWindow.show()
    def text_changed(self, value):
        self.lblResult.setText("Connection Established")

    def text_changed_Password(self):
        if self.lineEdit_Password.text() == "":
            self.btnLogin.setEnabled(False)
        else:
            self.btnLogin.setEnabled(True)


    def text_hide(self):
        self.lineEdit_Password.setEchoMode(QtGui.QLineEdit.Password)



class ControlUi_Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(ControlUi_Dialog, self).__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.initUI(self) 



    def closeEvent(self,event):
        result = QtGui.QMessageBox.critical(self,
                  "Confirm Exit...",
                  "Are you sure you want to exit ?",
                  QtGui.QMessageBox.Yes| QtGui.QMessageBox.No)
        event.ignore()

        if result == QtGui.QMessageBox.Yes:
            event.accept()


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    app.setStyle( "plastique" )
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

但是,我想知道:无论如何要为每个用户设置用户名和密码到他们的Windows登录用户名和密码?我一直在寻找文档,但我找不到任何东西。希望有人有答案。

1 个答案:

答案 0 :(得分:0)

看看这个Server Fault答案。它通过运行net use命令来检查用户是否有效。您可以使用子进程调用来运行外部终端命令,然后检查返回的错误代码以查看这是否是有效的凭据!