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

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐