- GtkModelButton
- 繼承關系
- Methods
- Virtual Methods
- Properties
- Signals
- 例子
- 附錄
- GtkButtonRole
Gtk.ModelButton
繼承關系
Gtk.ModelButton可以用一個Gio.Action作為其模型。當點選按鈕時,根據action-name來執行對應的方法。Gtk.ModelButton是Gtk.Button的直接子類
Methods
方法修飾詞 | 方法名及參數 |
---|---|
static | new () |
Virtual Methods
Properties
Name | Type | Flags | Short Description |
---|---|---|---|
active | bool | r/w/en | 是否選中 |
centered | bool | r/w/en | 内容是否居中 |
icon | Gio.Icon | r/w/en | 圖檔 |
iconic | bool | r/w/en | 是否是文字圖示 |
inverted | bool | r/w/en | 菜單是否是一個父容器 |
menu-name | str | r/w/en | 要打開菜單的名字 |
role | Gtk.ButtonRole | r/w/en | button樣式 |
text | str | r/w/en | 文本 |
Signals
Name | Short 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”的視窗
設定在目前類中處理ui中的信号
builder.connect_signals(self)
建立一個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