Pygtk-togglebutton-class
提供:Dev Guides
PyGTK-ToggleButtonクラス
ToggleButtonウィジェットは、 pressedまたはactive (またはon)状態と* normalまたはinactive(またはoff)*状態の2つの状態を持つgtk.Buttonです。 ボタンを押すたびに、状態が切り替わります。 トグルボタンの状態は、set_active()メソッドによってプログラムで変更することもできます。 ボタンの状態を切り替えるには、toggled()メソッドも使用できます。
gtk.ToggleButtonクラスには、次のコンストラクタがあります-
gtk.ToggleButton(label = None, use_underline = True)
ここで、ラベルはボタンに表示されるテストです。 use_underlineプロパティは、Trueの場合、テキスト内の下線は次の文字に下線を付けてニーモニックアクセラレータに使用することを示します。
gtk.ToggleButtonクラスの重要なメソッドのいくつかは、次の表に記載されています-
set_active() | This sets the active *property to the value to True (active or pressed or on) or False* (inactive or normal or off) |
get_active() | This retrieves the state of button |
toggled() | This emits the "toggled" signal on the togglebutton. |
トグルボタンウィジェットは、次の信号を発します-
Toggled | This is emitted when the togglebutton state changes either programmatically or by the user action. |
以下のコードは、ToggleButtonウィジェットの使用方法を示しています。
2つのToggleButtonsとLabelウィジェットがVBoxコンテナーに配置されます。 Button1によって発行されたトグル信号は、コールバック関数on_toggled()に接続されます。 この関数では、Button1の状態がFalseの場合、Button2の状態はTrueに設定されます。
if self.btn1.get_active() == True:
self.btn2.set_active(False)
else:
self.btn2.set_active(True)
ラベル上のボタンの瞬間的な状態を表示します。
例
次のコードを観察してください-
import gtk
PyApp(gtk.Window):
def __init__(self):
super(PyApp, self).__init__()
self.set_title("Toggle Button")
self.set_default_size(250, 200)
self.set_position(gtk.WIN_POS_CENTER)
vbox = gtk.VBox()
self.btn1 = gtk.ToggleButton("Button 1")
self.btn1.connect("toggled", self.on_toggled)
self.btn2 = gtk.ToggleButton("Button 2")
self.lbl = gtk.Label()
vbox.add(self.btn1)
vbox.add(self.btn2)
vbox.add(self.lbl)
self.add(vbox)
self.connect("destroy", gtk.main_quit)
self.show_all()
def on_toggled(self, widget, data = None):
if self.btn1.get_active() == True:
self.btn2.set_active(False)
else:
self.btn2.set_active(True)
state = "Button1 : "+str(self.btn1.get_active())+"
Button2 : "+str(self.btn2.get_active())
self.lbl.set_text(state)
if __name__ == '__main__':
PyApp()
gtk.main()
上記のコードは、次の出力を生成します-