PyGobject(十一)布局容器之Button篇——Gtk.RadioButton

Gtk.RadioButton

继承关系

Gtk.RadioButton复选框。Gtk.RadioButton是Gtk.CheckButton的直接子类
这里写图片描述

Methods

方法修饰词方法名及参数
staticnew (group)
staticnew_from_widget (radio_group_member)
staticnew_with_label (group, label)
staticnew_with_label_from_widget (radio_group_member, label)
staticnew_with_mnemonic (group, label)
staticnew_with_mnemonic_from_widget (radio_group_member, label)
get_group ()
join_group (group_source)
set_group (group)

Virtual Methods

do_group_changed ()

Properties

NameTypeFlagsShort Description
groupGtk.RadioButtonwRadioButton 所属的组别.

Signals

NameShort Description
group-changedRadioButton所属组别改变后,发送此信号.

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/14
# section 012
TITLE = "RadioButton"
DESCRIPTION = """
A single radio button performs the same basic function as a Gtk.CheckButton,
as its position in the object hierarchy reflects.
It is only when multiple radio buttons are grouped together
that they become a different user interface component in their own right
"""
import gi

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk


class RadioButtonWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="RadioButton Demo")
        self.set_border_width(10)

        grid = Gtk.Grid()
        self.add(grid)

        button1 = Gtk.RadioButton.new_with_label_from_widget(None, "Button 1")
        button1.connect("toggled", self.on_button_toggled, "1")
        grid.attach(button1, 0, 0, 1, 1)

        button2 = Gtk.RadioButton.new_from_widget(button1)
        button2.set_label("Button 2")
        button2.connect("toggled", self.on_button_toggled, "2")
        grid.attach(button2, 1, 0, 1, 1)

        # press alt+u
        button3 = Gtk.RadioButton.new_with_mnemonic_from_widget(button1,
                                                                "B_utton 3")
        button3.connect("toggled", self.on_button_toggled, "3")
        grid.attach(button3, 2, 0, 1, 1)

        button4 = Gtk.RadioButton.new_with_label(None, "Button4")
        button4.connect("toggled", self.on_button_toggled, "4")
        grid.attach(button4, 0, 1, 1, 1)
        button5 = Gtk.RadioButton.new_with_label(None, "Button5")
        button5.connect("toggled", self.on_button_toggled, "5")
        grid.attach(button5, 1, 1, 1, 1)
        button5.join_group(button4)

        # press alt+b
        button6 = Gtk.RadioButton.new_with_mnemonic(None, "_Button6")
        button6.connect("toggled", self.on_button_toggled, "6")
        button6.join_group(button5)
        grid.attach(button6, 2, 1, 1, 1)

        # button2.set_active(True)

    @staticmethod
    def on_button_toggled(button, name):
        if button.get_active():
            state = "on"
        else:
            state = "off"
        print("Button", name, "was turned", state)


def main():
    win = RadioButtonWindow()
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()


if __name__ == "__main__":
    main()

这个例子中有两组RadioButton,Button1,2,3为一组,剩下的为另一组。

button1 = Gtk.RadioButton.new_with_label_from_widget(None, "Button 1")

Button1使用new_with_label_from_widget()方法创建,并且第一个参数为None,那么它就是新的一个组。

button2 = Gtk.RadioButton.new_from_widget(button1)

button2使用new_from_widget()方法创建,并且传入button1为参数,意思是将button2添加到button1这个组中。

button3 = Gtk.RadioButton.new_with_mnemonic_from_widget(button1,
                                                                "B_utton 3")

button3的创建与button2创建的不同之处就是,button3使用了助记符,当按下alt+u时,选中button3.



第二组的主要是使用new_with_label(None,”“)方法创建,使用
join_group()方法,将一个button添加到另一个button所在的小组中

button5.join_group(button4)





代码下载地址: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、付费专栏及课程。

余额充值