PyGobject(九)布局容器之Button篇——Gtk.ToggleButton

Gtk.ToggleButton

继承关系

Gtk.ToggleButton是带有开和关两种状态的按钮。Gtk.ToggleButton是Gtk.Button的直接子类

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

Methods

方法修饰词方法名及参数
staticnew ()
staticnew_with_label (label)
staticnew_with_mnemonic (label)
get_active ()
get_inconsistent ()
get_mode ()
set_active (is_active)
set_inconsistent (setting)
set_mode (draw_indicator)
toggled ()

Virtual Methods

do_toggled ()

Properties

NameTypeFlagsShort Description
activeboolr/w/entoggle button是否按下,True按下
draw-indicatorboolr/w在它的子类中使用(如checkbutton),
如果设置为False,则checkbutton不显示复选框
inconsistentboolr/w/en设置一个中间状态,只影响外观,不影响实际的值。
如checkbutton,如果设置inconsistent为true,
则复选框会显示一条横线。

Signals

NameShort Description
toggled状态改变时发送此信号

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/14
# section 010
TITLE = "ToggleButton"
DESCRIPTION = """
A Gtk.ToggleButton is a Gtk.Button which will remain “pressed-in” when clicked.
Clicking again will cause the toggle button to return to its normal state
"""
import gi

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


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

        hbox = Gtk.Box(spacing=6)
        self.add(hbox)

        button = Gtk.ToggleButton("Button 1")
        button.connect("toggled", self.on_button_toggled, "1")
        hbox.pack_start(button, True, True, 0)

        button = Gtk.ToggleButton("B_utton 2", use_underline=True)
        button.set_active(True)
        button.connect("toggled", self.on_button_toggled, "2")
        hbox.pack_start(button, True, True, 0)

    @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 = ToggleButtonWindow()
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()


if __name__ == "__main__":
    main()

创建两个Gtk.ToggleButton,都绑定“toggled”信号到on_button_toggled方法,并且分别传递参数“1”和“2”

button = Gtk.ToggleButton("Button 1")
        button.connect("toggled", self.on_button_toggled, "1")

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

使用get_active()方法获知ToggleButton当前状态,如果返回值为True则表示开启状态,如果为False表示关闭状态





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

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sanxiaochengyu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值