PyGobject(十四)布局容器之Button篇——Gtk.LockButton

Gtk.LockButton

继承关系

Gtk.LockButton锁定与解锁按钮,常用来进行权限管理。Gtk.LockButton是Gtk.Button的直接子类
这里写图片描述

Methods

方法修饰词方法名及参数
staticnew (permission)
get_permission ()
set_permission (permission)

Virtual Methods

Properties

NameTypeFlagsShort Description
permissionGio.Permissionr/wThe Gio.Permission object controlling this button
text-lockstrr/w/c锁定状态显示的文本
text-unlockstrr/w/c解锁状态显示的文本
tooltip-lockstrr/w/c提示用户锁定时显示的文本
tooltip-not-authorizedstrr/w/c提示用户不能获得授权时显示的文本
tooltip-unlockstrr/w/c提示用户解锁时显示的文本

Signals

NameShort 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.csdn.net/detail/a87b01c14/9594728

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanxiaochengyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值