Gtk.ToggleButton
继承关系
Gtk.ToggleButton是带有开和关两种状态的按钮。Gtk.ToggleButton是Gtk.Button的直接子类
Methods
方法修饰词 | 方法名及参数 |
---|---|
static | new () |
static | new_with_label (label) |
static | new_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
Name | Type | Flags | Short Description |
---|---|---|---|
active | bool | r/w/en | toggle button是否按下,True按下 |
draw-indicator | bool | r/w | 在它的子类中使用(如checkbutton), 如果设置为False,则checkbutton不显示复选框 |
inconsistent | bool | r/w/en | 设置一个中间状态,只影响外观,不影响实际的值。 如checkbutton,如果设置inconsistent为true, 则复选框会显示一条横线。 |
Signals
Name | Short 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表示关闭状态