Gtk.LockButton
继承关系
Gtk.LockButton锁定与解锁按钮,常用来进行权限管理。Gtk.LockButton是Gtk.Button的直接子类
Methods
方法修饰词 | 方法名及参数 |
---|---|
static | new (permission) |
get_permission () | |
set_permission (permission) |
Virtual Methods
Properties
Name | Type | Flags | Short Description |
---|---|---|---|
permission | Gio.Permission | r/w | The Gio.Permission object controlling this button |
text-lock | str | r/w/c | 锁定状态显示的文本 |
text-unlock | str | r/w/c | 解锁状态显示的文本 |
tooltip-lock | str | r/w/c | 提示用户锁定时显示的文本 |
tooltip-not-authorized | str | r/w/c | 提示用户不能获得授权时显示的文本 |
tooltip-unlock | str | r/w/c | 提示用户解锁时显示的文本 |
Signals
Name | Short Description |
---|
例子
代码:
#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/27
# section 015
TITLE = "LockButton"
DESCRIPTION = """
Gtk.LockButton is a widget that can be used in control panels or preference dialogs
to allow users to obtain and revoke authorizations needed to operate the controls.
The required authorization is represented by a Gio.Permission object.
Concrete implementations of Gio.Permission may use PolicyKit or some other authorization framework.
To obtain a PolicyKit-based Gio.Permission, use polkit_permission_new().
"""
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, Gio, GObject
class LockButtonWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="LockButton Demo")
self.set_border_width(10)
parent = Gtk.HBox(spacing=6)
self.box = Gtk.HBox(spacing=6, homogeneous=False)
self.check_button = Gtk.CheckButton("check button")
self.box.pack_start(self.check_button, False, False, 0)
self.box.pack_start(Gtk.Button("button"), False, False, 0)
circular_button = Gtk.Button.new_from_icon_name("emblem-system-symbolic", Gtk.IconSize.MENU)
circular_button.get_style_context().add_class("circular")
self.box.pack_start(circular_button, False, False, 0)
lbtn = Gtk.LockButton()
lbtn.connect("clicked", self.on_clicked)
permission = Gio.SimplePermission.new(allowed=True)
permission.impl_update(True, True, True)
permission.bind_property("allowed", self.box, "sensitive", GObject.BindingFlags.SYNC_CREATE)
lbtn.set_permission(permission)
parent.pack_start(self.box, False, False, 0)
parent.pack_start(lbtn, False, False, 0)
self.add(parent)
def on_clicked(self, button):
if button.get_permission().get_allowed():
self.box.props.sensitive = False
button.get_permission().impl_update(False, True, True)
else:
self.box.props.sensitive = True
button.get_permission().impl_update(True, True, True)
def main():
win = LockButtonWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
分析主要代码
创建一个LockButton,绑定”clicked”事件
lbtn = Gtk.LockButton()
lbtn.connect("clicked", self.on_clicked)
创建Gio.SimplePermission对象,将permission的“allowed”属性与self.box的“sensitive”属性绑定
permission = Gio.SimplePermission.new(allowed=True)
permission.impl_update(True, True, True)
permission.bind_property("allowed", self.check_button, "sensitive", GObject.BindingFlags.SYNC_CREATE)
impl_update(allowed, can_acquire, can_release)[source]
Parameters:
allowed (bool) – ‘allowed’ property
can_acquire (bool) – the new value for the ‘can-acquire’ property
can_release (bool) – the new value for the ‘can-release’ property
设置LockButton的权限控制对象
lbtn.set_permission(permission)
/(ㄒoㄒ)/~~
无赖,我执行此代码一直显示
”Error releasing permission: Can’t acquire or release permission”
只好在点击事件中做文章
def on_clicked(self, button):
if button.get_permission().get_allowed():
self.box.props.sensitive = False
button.get_permission().impl_update(False, True, True)
else:
self.box.props.sensitive = True
button.get_permission().impl_update(True, True, True)
代码下载地址:http://download.youkuaiyun.com/detail/a87b01c14/9594728