-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathpassword.py
More file actions
56 lines (43 loc) · 1.84 KB
/
password.py
File metadata and controls
56 lines (43 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import sys
if 'PyQt5' in sys.modules:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtCore import pyqtSignal as Signal
from . import resources_pyqt5
elif 'PySide2' in sys.modules:
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtCore import Qt
from PySide2.QtCore import Signal
from . import resources_pyside2
else:
from PySide6 import QtCore, QtGui, QtWidgets
from PySide6.QtCore import Qt
from PySide6.QtCore import Signal
#from . import resources_pyside6
class PasswordEdit(QtWidgets.QLineEdit):
"""
Password LineEdit with icons to show/hide password entries.
Based on this example https://kushaldas.in/posts/creating-password-input-widget-in-pyqt.html by Kushal Das.
"""
def __init__(self, show_visibility=True, *args, **kwargs):
super().__init__(*args, **kwargs)
self.visibleIcon = QtGui.QIcon(":/icons/eye.svg")
self.hiddenIcon = QtGui.QIcon(":/icons/hidden.svg")
self.setEchoMode(QtWidgets.QLineEdit.Password)
if show_visibility:
# Add the password hide/shown toggle at the end of the edit box.
self.togglepasswordAction = self.addAction(
self.visibleIcon,
QtWidgets.QLineEdit.TrailingPosition
)
self.togglepasswordAction.triggered.connect(self.on_toggle_password_Action)
self.password_shown = False
def on_toggle_password_Action(self):
if not self.password_shown:
self.setEchoMode(QtWidgets.QLineEdit.Normal)
self.password_shown = True
self.togglepasswordAction.setIcon(self.hiddenIcon)
else:
self.setEchoMode(QtWidgets.QLineEdit.Password)
self.password_shown = False
self.togglepasswordAction.setIcon(self.visibleIcon)