Created
February 21, 2024 15:56
-
-
Save promto-c/3c65aa5027042aee572bbc98f551b317 to your computer and use it in GitHub Desktop.
Secure PyQt5 Login Widget with Credential Saving Feature using Keyring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| from PyQt5 import QtWidgets, QtCore | |
| import keyring | |
| class LoginWidget(QtWidgets.QWidget): | |
| def __init__(self): | |
| super().__init__() | |
| self.initUI() | |
| self.load_credentials() | |
| def initUI(self): | |
| self.setWindowTitle('Login') | |
| self.layout = QtWidgets.QVBoxLayout() | |
| # Username field | |
| self.username = QtWidgets.QLineEdit(self) | |
| self.username.setPlaceholderText('Username') | |
| self.layout.addWidget(self.username) | |
| # Password field | |
| self.password = QtWidgets.QLineEdit(self) | |
| self.password.setPlaceholderText('Password') | |
| self.password.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password) | |
| self.layout.addWidget(self.password) | |
| # Checkbox for saving credentials | |
| self.save_credentials = QtWidgets.QCheckBox('Save credentials', self) | |
| self.layout.addWidget(self.save_credentials) | |
| # Login button | |
| self.login_button = QtWidgets.QPushButton('Login', self) | |
| self.login_button.clicked.connect(self.handle_login) | |
| self.layout.addWidget(self.login_button) | |
| self.setLayout(self.layout) | |
| def load_credentials(self): | |
| # Attempt to load the username; use a fixed key or identifier for the username | |
| saved_username = keyring.get_password('MyApp', 'saved_username') | |
| if saved_username: | |
| # If there's a saved username, attempt to load the corresponding password | |
| saved_password = keyring.get_password('MyApp', saved_username) | |
| if saved_password: | |
| self.username.setText(saved_username) | |
| self.password.setText(saved_password) | |
| self.save_credentials.setChecked(True) | |
| def handle_login(self): | |
| username = self.username.text() | |
| password = self.password.text() | |
| if self.save_credentials.isChecked(): | |
| # Save the username using a fixed key or identifier | |
| keyring.set_password('MyApp', 'saved_username', username) | |
| # Save the password using the username as the key | |
| keyring.set_password('MyApp', username, password) | |
| print("Credentials saved.") | |
| else: | |
| # Optionally, clear the saved credentials if the checkbox is not checked | |
| keyring.delete_password('MyApp', 'saved_username') | |
| keyring.delete_password('MyApp', username) | |
| print("Credentials not saved.") | |
| # Add your login verification logic here | |
| print(f"Attempting to login with username: {username} and password: {password}") | |
| # Clear fields after login attempt (optional) | |
| self.username.clear() | |
| self.password.clear() | |
| def main(): | |
| app = QtWidgets.QApplication(sys.argv) | |
| login_widget = LoginWidget() | |
| login_widget.show() | |
| sys.exit(app.exec_()) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment