LockButton

A LockButton provides a way for a user to make changes to system settings that require elevated privileges. The widget is packed with a label and icon identifying the unlock and lock actions.

Constructor

The LockButton can be constructed using the following:

lockbutton = Gtk.LockButton()

Methods

To set the permission of the LockButton use the method:

lockbutton.set_permission(permission)

The permission value can also be retrived from the LockButton via:

permission = lockbutton.get_permission()

Example

Below is an example of a LockButton:

#!/usr/bin/env python3

from gi.repository import Gtk, GObject, Polkit, Gio
import os

def check_authorization(lockbutton):
    authority.check_authorization(subject, action_id, None, Polkit.CheckAuthorizationFlags.ALLOW_USER_INTERACTION, cancellable, check_authorization_cb, mainloop)

def check_authorization_cb(authority, res, loop):
    try:
        result = authority.check_authorization_finish(res)
        if result.get_is_authorized():
            print("Authorized")
        elif result.get_is_challenge():
            print("Challenge")
        else:
            print("Not authorized")
    except GObject.GError as error:
         print("Error checking authorization: %s" % error.message)

if __name__ == "__main__":
    action_id = "org.freedesktop.policykit.exec"

    mainloop = GObject.MainLoop()
    authority = Polkit.Authority.get()
    cancellable = Gio.Cancellable()
    subject = Polkit.UnixProcess.new(os.getppid())

    window = Gtk.Window()
    window.connect("destroy", Gtk.main_quit)

    lockbutton = Gtk.LockButton()
    lockbutton.connect("clicked", check_authorization)
    window.add(lockbutton)

    window.show_all()

    mainloop.run()

Download: LockButton