Skip to content

Commit 09b40b6

Browse files
committed
Add PasswordEdit widget, with toggleable visiblity.
1 parent 3a85417 commit 09b40b6

File tree

6 files changed

+284
-0
lines changed

6 files changed

+284
-0
lines changed

.gitignore

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Created by https://www.gitignore.io
2+
3+
### OSX ###
4+
.DS_Store
5+
.AppleDouble
6+
.LSOverride
7+
8+
# Icon must end with two \r
9+
Icon
10+
11+
12+
# Thumbnails
13+
._*
14+
15+
# Files that might appear on external disk
16+
.Spotlight-V100
17+
.Trashes
18+
19+
# Directories potentially created on remote AFP share
20+
.AppleDB
21+
.AppleDesktop
22+
Network Trash Folder
23+
Temporary Items
24+
.apdisk
25+
26+
27+
### Python ###
28+
# Byte-compiled / optimized / DLL files
29+
__pycache__/
30+
*.py[cod]
31+
32+
# C extensions
33+
*.so
34+
35+
# Distribution / packaging
36+
.Python
37+
env/
38+
build/
39+
develop-eggs/
40+
dist/
41+
downloads/
42+
eggs/
43+
lib/
44+
lib64/
45+
parts/
46+
sdist/
47+
var/
48+
*.egg-info/
49+
.installed.cfg
50+
*.egg
51+
52+
# PyInstaller
53+
# Usually these files are written by a python script from a template
54+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
55+
*.manifest
56+
*.spec
57+
58+
# Installer logs
59+
pip-log.txt
60+
pip-delete-this-directory.txt
61+
62+
# Unit test / coverage reports
63+
htmlcov/
64+
.tox/
65+
.coverage
66+
.cache
67+
nosetests.xml
68+
coverage.xml
69+
70+
# Translations
71+
*.mo
72+
*.pot
73+
74+
# Sphinx documentation
75+
docs/_build/
76+
77+
# PyBuilder
78+
target/
79+
80+
81+
### Django ###
82+
*.log
83+
*.pot
84+
*.pyc
85+
__pycache__/
86+
local_settings.py
87+
88+
.env
89+
db.sqlite3
90+
91+
static/**
92+
93+
media/**
94+
95+
96+

passwordedit/demo_pyqt5.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from PyQt5 import QtCore, QtGui, QtWidgets
2+
from password import PasswordEdit
3+
4+
5+
class Window(QtWidgets.QMainWindow):
6+
7+
def __init__(self):
8+
super().__init__()
9+
10+
password = PasswordEdit()
11+
self.setCentralWidget(password)
12+
13+
14+
app = QtWidgets.QApplication([])
15+
w = Window()
16+
w.show()
17+
app.exec_()
18+
19+
20+
21+
22+

passwordedit/demo_pyside2.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from PySide2 import QtCore, QtGui, QtWidgets
2+
from password import PasswordEdit
3+
4+
5+
class Window(QtWidgets.QMainWindow):
6+
7+
def __init__(self):
8+
super().__init__()
9+
10+
password = PasswordEdit()
11+
self.setCentralWidget(password)
12+
13+
14+
app = QtWidgets.QApplication([])
15+
w = Window()
16+
w.show()
17+
app.exec_()
18+
19+
20+
21+
22+

passwordedit/eye.svg

Lines changed: 46 additions & 0 deletions
Loading

passwordedit/hidden.svg

Lines changed: 50 additions & 0 deletions
Loading

passwordedit/password.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import sys
2+
if 'PyQt5' in sys.modules:
3+
from PyQt5 import QtCore, QtGui, QtWidgets
4+
from PyQt5.QtCore import Qt
5+
from PyQt5.QtCore import pyqtSignal as Signal
6+
7+
else:
8+
from PySide2 import QtCore, QtGui, QtWidgets
9+
from PySide2.QtCore import Qt
10+
from PySide2.QtCore import Signal
11+
12+
13+
class PasswordEdit(QtWidgets.QLineEdit):
14+
"""
15+
Password LineEdit with icons to show/hide password entries.
16+
"""
17+
18+
def __init__(self, show_visibility=True, *args, **kwargs):
19+
super().__init__(*args, **kwargs)
20+
21+
self.visibleIcon = QtGui.QIcon("eye.svg")
22+
self.hiddenIcon = QtGui.QIcon("hidden.svg")
23+
24+
self.setEchoMode(QtWidgets.QLineEdit.Password)
25+
26+
if show_visibility:
27+
# Add the password hide/shown toggle at the end of the edit box.
28+
self.togglepasswordAction = self.addAction(
29+
self.visibleIcon,
30+
QtWidgets.QLineEdit.TrailingPosition
31+
)
32+
self.togglepasswordAction.triggered.connect(self.on_toggle_password_Action)
33+
34+
self.password_shown = False
35+
36+
def on_toggle_password_Action(self):
37+
if not self.password_shown:
38+
self.setEchoMode(QtWidgets.QLineEdit.Normal)
39+
self.password_shown = True
40+
self.togglepasswordAction.setIcon(self.hiddenIcon)
41+
else:
42+
self.setEchoMode(QtWidgets.QLineEdit.Password)
43+
self.password_shown = False
44+
self.togglepasswordAction.setIcon(self.visibleIcon)
45+
46+
47+
48+

0 commit comments

Comments
 (0)