PyGobject(十七)布局容器之Button篇——Gtk.ModelButton

Gtk.ModelButton

继承关系

Gtk.ModelButton可以用一个Gio.Action作为其模型。当点击按钮时,根据action-name来执行对应的方法。Gtk.ModelButton是Gtk.Button的直接子类
这里写图片描述

Methods

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

Virtual Methods

Properties

NameTypeFlagsShort Description
activeboolr/w/en是否选中
centeredboolr/w/en内容是否居中
iconGio.Iconr/w/en图片
iconicboolr/w/en是否是文字图标
invertedboolr/w/en菜单是否是一个父容器
menu-namestrr/w/en要打开菜单的名字
roleGtk.ButtonRoler/w/enbutton样式
textstrr/w/en文本

Signals

NameShort Description

例子

这里写图片描述
这里写图片描述

这个例子的布局文件是通过glade生成的。
modelbutton.glade

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.6 -->
  <object class="GtkWindow" id="window1">
    <child type="titlebar">
      <object class="GtkHeaderBar">
        <property name="visible">1</property>
        <property name="show-close-button">1</property>
        <property name="title" translatable="yes">Model Button</property>
      </object>
    </child>
    <child>
      <object class="GtkBox">
        <property name="visible">1</property>
        <property name="orientation">vertical</property>
        <property name="margin">80</property>
        <child>
          <object class="GtkMenuButton">
            <property name="visible">1</property>
            <property name="popover">thing_a</property>
            <child>
              <object class="GtkLabel">
                <property name="visible">1</property>
                <property name="label">Color</property>
                <property name="hexpand">1</property>
              </object>
            </child>
          </object>
        </child>
        <child>
          <object class="GtkMenuButton">
            <property name="visible">1</property>
            <property name="popover">thing_b</property>
            <child>
              <object class="GtkLabel">
                <property name="visible">1</property>
                <property name="label">Flavors</property>
                <property name="hexpand">1</property>
              </object>
            </child>
          </object>
        </child>
        <child>
          <object class="GtkMenuButton">
            <property name="visible">1</property>
            <property name="popover">thing_c</property>
            <child>
              <object class="GtkLabel">
                <property name="visible">1</property>
                <property name="label">Tools</property>
                <property name="hexpand">1</property>
              </object>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkPopover" id="thing_a">
    <child>
      <object class="GtkBox">
        <property name="visible">1</property>
        <property name="margin">10</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="action-name">win.color</property>
            <property name="action-target">'red'</property>
            <property name="text">Red</property>
            <property name="inverted">1</property>
          </object>
        </child>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="action-name">win.color</property>
            <property name="action-target">'green'</property>
            <property name="text">Green</property>
            <property name="inverted">1</property>
          </object>
        </child>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="action-name">win.color</property>
            <property name="action-target">'blue'</property>
            <property name="text">Blue</property>
            <property name="inverted">1</property>
          </object>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkPopover" id="thing_b">
    <child>
      <object class="GtkBox">
        <property name="visible">1</property>
        <property name="margin">10</property>
        <property name="orientation">vertical</property>
        <property name="spacing">10</property>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="action-name">win.chocolate</property>
            <property name="text">Chocolate</property>
          </object>
        </child>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="action-name">win.vanilla</property>
            <property name="text">Vanilla</property>
          </object>
        </child>
        <child>
          <object class="GtkSeparator">
            <property name="visible">1</property>
          </object>
        </child>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="action-name">win.sprinkles</property>
            <property name="text">Add Sprinkles</property>
          </object>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkPopover" id="thing_c">
    <child>
      <object class="GtkBox">
        <property name="visible">1</property>
        <property name="margin">10</property>
        <property name="orientation">vertical</property>
        <property name="spacing">10</property>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="text">Hammer</property>
            <property name="role">check</property>
            <signal name="clicked" handler="tool_clicked"/>
          </object>
        </child>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="text">Screwdriver</property>
            <property name="role">check</property>
            <signal name="clicked" handler="tool_clicked"/>
          </object>
        </child>
        <child>
          <object class="GtkModelButton">
            <property name="visible">1</property>
            <property name="text">Drill</property>
            <property name="role">check</property>
            <signal name="clicked" handler="tool_clicked"/>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>

代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/14
# section 018
TITLE = "ModelButton"
DESCRIPTION = """
Gtk.ModelButton is a button class that can use a Gio.Action as its model.
"""
import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GLib, Gio
import os

win_entries = ("color", "chocolate", "vanilla", "sprinkles")


class ModelButtonWindow():
    def __init__(self):
        builder = Gtk.Builder.new_from_file(os.path.join(os.path.dirname(__file__), "../../../Data/modelbutton.glade"))
        window = builder.get_object("window1")
        builder.connect_signals(self)
        actions = Gio.SimpleActionGroup()
        for entry in win_entries:
            action = Gio.SimpleAction.new(entry, GLib.VariantType.new("s") if entry == "color" else None)
            action.connect("activate", self.do_something)
            actions.insert(action)
        window.insert_action_group("win", actions)
        window.connect("destroy", Gtk.main_quit)
        window.show_all()
        Gtk.main()

    @staticmethod
    def tool_clicked(button):
        active = button.props.active
        button.props.active = not active

    @staticmethod
    def do_something(action, parameter):
        parameter_type = action.get_parameter_type()
        type_string = ""
        if parameter_type:
            type_string = parameter_type.dup_string()
        print(action.get_name(), type_string, parameter)


def main():
    ModelButtonWindow()


if __name__ == "__main__":
    main()

读取ui配置文件

builder = Gtk.Builder.new_from_file(os.path.join(os.path.dirname(__file__), "../../../Data/modelbutton.glade"))

从builder中获取id=”window1”的窗口

window = builder.get_object("window1")

设置在当前类中处理ui中的信号

builder.connect_signals(self)

创建一个Gio.SimpleActionGroup(),用来处理选项的点击事件

actions = Gio.SimpleActionGroup()

创建四个Gio.SimpleAction,将其“activate”信号绑定到do_something()方法,并添加到Gio.SimpleActionGroup组中。

win_entries = ("color", "chocolate", "vanilla", "sprinkles")
for entry in win_entries:
    action = Gio.SimpleAction.new(entry, 
    GLib.VariantType.new("s") if entry == "color" else None)
    action.connect("activate", self.do_something)
    actions.insert(action)


 @staticmethod
def do_something(action, parameter):
    parameter_type = action.get_parameter_type()
    type_string = ""
    if parameter_type:
        type_string = parameter_type.dup_string()
    print(action.get_name(), type_string, parameter)

设置window的Gio.ActionGroup

 window.insert_action_group("win", actions)

点击各item时打印信息,如下

color s 'red'
color s 'green'
color s 'blue'
sprinkles  None
vanilla  None
chocolate  None

附录

Gtk.ButtonRole

class Gtk.ButtonRole
Bases: GObject.GEnum
Gtk.ModelButton的样式

NORMAL = 0

普通按钮

CHECK = 1

CheckButton

RADIO = 2

RadioButton




代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sanxiaochengyu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值