Created
February 10, 2016 21:40
-
-
Save kived/610386b5181219622e33 to your computer and use it in GitHub Desktop.
Kivy: custom settings password entry
This file contains 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
[ | |
{"type": "password", | |
"title": "PASSWORD", | |
"section": "test", | |
"key": "pw"} | |
] |
This file contains 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 kivy | |
kivy.require('1.8.1') | |
from kivy.app import App | |
from kivy.lang import Builder | |
from kivy.uix.settings import SettingString | |
from kivy.uix.label import Label | |
class PasswordLabel(Label): | |
pass | |
root = Builder.load_string(''' | |
<SettingPassword>: | |
PasswordLabel: | |
text: '(set)' if root.value else '(unset)' | |
pos: root.pos | |
font_size: '15sp' | |
Button: | |
on_release: app.open_settings() | |
''') | |
class SettingPassword(SettingString): | |
def _create_popup(self, instance): | |
super(SettingPassword, self)._create_popup(instance) | |
self.textinput.password = True | |
def add_widget(self, widget, *largs): | |
if self.content is None: | |
super(SettingString, self).add_widget(widget, *largs) | |
if isinstance(widget, PasswordLabel): | |
return self.content.add_widget(widget, *largs) | |
class TestApp(App): | |
def build(self): | |
return root | |
def build_config(self, config): | |
config.setdefaults('test', {'pw': ''}) | |
print 'pw:', config.get('test', 'pw') | |
def build_settings(self, settings): | |
settings.register_type('password', SettingPassword) | |
settings.add_json_panel('Settings', self.config, filename='settings.json') | |
if __name__ == '__main__': | |
TestApp().run() |
I want to encrypt passwords rather than storing them in plain text in the .ini file, so I added a _validate method to the class. This gets called automatically when "OK" button is clicked. This is just a placeholder -- the encryption function is a separate exercise.
> class SettingPassword(SettingString):
> def _create_popup(self, instance):
> super(SettingPassword, self)._create_popup(instance)
> self.textinput.password = True
>
def _validate(self, instance):
self._dismiss()
value = self.textinput.text.strip()
self.value = value + "Encrypted" #Change this to your encryption
print(self.value) #Just for debugging
> def add_widget(self, widget, *largs):
> if self.content is None:
> super(SettingString, self).add_widget(widget, *largs)
> if isinstance(widget, PasswordLabel):
> return self.content.add_widget(widget, *largs)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much for this gist.
Here is if you want to show the '*' in Label for password: