1
- import sys
2
1
import os
2
+ import sys
3
+
3
4
from qtpy import QtCore , QtGui , QtWidgets
4
- from qtpy .QtCore import Qt
5
- from qtpy .QtCore import Signal
5
+ from qtpy .QtCore import Qt , Signal
6
6
7
7
folder = os .path .dirname (__file__ )
8
8
9
+
9
10
class PasswordEdit (QtWidgets .QLineEdit ):
10
11
"""
11
12
Password LineEdit with icons to show/hide password entries.
12
13
Based on this example https://kushaldas.in/posts/creating-password-input-widget-in-pyqt.html by Kushal Das.
13
14
"""
14
15
15
- def __init__ (self , show_visibility = True , * args , ** kwargs ):
16
+ def __init__ (
17
+ self ,
18
+ show_visibility = True ,
19
+ visible_icon = None ,
20
+ hidden_icon = None ,
21
+ icons_from_theme = True ,
22
+ * args ,
23
+ ** kwargs
24
+ ):
16
25
super ().__init__ (* args , ** kwargs )
17
26
18
- self .visibleIcon = QtGui .QIcon (os .path .join (folder , "eye.svg" ))
19
- self .hiddenIcon = QtGui .QIcon (os .path .join (folder , "hidden.svg" ))
27
+ if icons_from_theme :
28
+ self .visibleIcon = QtGui .QIcon .fromTheme ("view-visible" )
29
+ self .hiddenIcon = QtGui .QIcon .fromTheme ("view-hidden" )
30
+ else :
31
+ if visible_icon :
32
+ self .visibleIcon = visible_icon
33
+ else :
34
+ self .visibleIcon = QtGui .QIcon (os .path .join (folder , "eye.svg" ))
35
+ if hidden_icon :
36
+ self .hiddenIcon = hidden_icon
37
+ else :
38
+ self .hiddenIcon = QtGui .QIcon (
39
+ os .path .join (folder , "hidden.svg" )
40
+ )
20
41
21
42
self .setEchoMode (QtWidgets .QLineEdit .Password )
22
43
23
44
if show_visibility :
24
45
# Add the password hide/shown toggle at the end of the edit box.
25
46
self .togglepasswordAction = self .addAction (
26
- self .visibleIcon ,
27
- QtWidgets .QLineEdit .TrailingPosition
47
+ self .visibleIcon , QtWidgets .QLineEdit .TrailingPosition
48
+ )
49
+ self .togglepasswordAction .triggered .connect (
50
+ self .on_toggle_password_Action
28
51
)
29
- self .togglepasswordAction .triggered .connect (self .on_toggle_password_Action )
30
52
31
53
self .password_shown = False
32
54
@@ -39,7 +61,3 @@ def on_toggle_password_Action(self):
39
61
self .setEchoMode (QtWidgets .QLineEdit .Password )
40
62
self .password_shown = False
41
63
self .togglepasswordAction .setIcon (self .visibleIcon )
42
-
43
-
44
-
45
-
0 commit comments