Skip to content

Instantly share code, notes, and snippets.

@drydenp

drydenp/pwdialog.py

Created May 13, 2018
Embed
What would you like to do?
Graphical Password Dialog along the likes of Zenity and Yad, only a bit prettier
#!/usr/bin/python
import gi
import sys
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GLib
def log_handler(a, b, c, d):
pass
GLib.log_set_handler("Gtk", GLib.LogLevelFlags.LEVEL_WARNING, log_handler, None);
class PasswordBox(Gtk.Dialog):
def ok_click(self, button):
print(self.entry.get_text());
exit(0);
def esc(self, boo):
exit(1);
def cancel_click(self, button):
exit(1);
def entry_enter(self, entry):
self.ok.activate();
def __init__(self, label):
Gtk.Dialog.__init__(self, title = "Password Entry")
# Oops
fake = Gtk.Window();
self.set_transient_for(fake);
self.set_default_size(480,100);
content = self.get_content_area();
align = Gtk.Alignment();
align.set_padding(16,11,8,8);
vbox = Gtk.VBox(0);
la = Gtk.Alignment();
la.set_padding(12,15,50,0);
l = Gtk.Label(halign = Gtk.Align.START);
l.set_markup("<span size=\"10880\">Enter password for %s</span>" % label);
la.add(l);
vbox.pack_start(la, True, False, 0);
# using hbox to reduce the size of the entry
hpad = Gtk.Alignment();
hpad.set_padding(0,0,25,25);
entry = Gtk.Entry(visibility = False);
hpad.add(entry);
vbox.pack_start(hpad, True, False, 0);
align.add(vbox);
content.add(align);
action = self.get_action_area();
labels = { "ok": "_Ok", "cancel": "_Cancel" }
buttons = {};
fontsize = 10820;
for b, text in labels.items():
L = "<span size=\"%d\">%s</span>" % (fontsize, text);
button = Gtk.Button();
bl = Gtk.Label();
bl.set_markup_with_mnemonic(L);
bin = Gtk.Alignment();
bin.add(bl);
bin.set_padding(1,1,10,10);
button.add(bin);
buttons[b] = button;
apad = Gtk.Alignment();
apad.set_padding(4,8,8,8);
h = Gtk.HBox(spacing = 2, homogeneous = True);
h.pack_start(buttons["ok"], True, True, 5);
h.pack_start(buttons["cancel"], True, True, 5);
apad.add(h);
action.add(apad);
buttons["ok"].connect("clicked", self.ok_click);
buttons["cancel"].connect( "clicked", self.cancel_click );
entry.connect( "activate", self.entry_enter);
self.entry = entry;
self.ok = buttons["ok"];
self.connect("destroy", self.esc);
if len(sys.argv) == 1:
print("Needs an argument");
exit(1);
pwdialog = PasswordBox(sys.argv[1]);
pwdialog.show_all();
Gtk.main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment