pyGTK_读书笔记_Tutorial Chapter 5&6_Widget Overview and Button Widget

来源:互联网 发布:淘宝客源码免费下载 编辑:程序博客网 时间:2024/05/29 17:09

在pyGTK中使用部件的通用步骤:

1, 利用gtk.*创建一个部件对象;

2, 将我们需要使用的signal和event关联到相应的handler;

3, 配置部件的属性;

4, 调用gtk.Container.add()或者gtk.Box.pack_start()将部件包入容器;

5, 调用gtk.Widget.Show()保证在窗口上显示部件。


Buttion Widget: Normal Button;Toggle Button;Check Button;Radio Button


1, Normal Button

创建:button = gtk.Button(label=None, stock=None)

label: 定义按钮上的显示的文字;

stock: 将按钮的显示关联到库中的图标和文本。

Normal Button部件有如下的signal: pressed; released; clicked; enter; leave.


2, Toggle Button

Toggle Button有两种状态,点击一次,会呈现按下的状态;再次点击,则弹起。

创建:toggle_button = gtk.ToggleButton(label=None)

实例:

        vbox = gtk.VBox(True, 2)
        self.window.add(vbox)
        
        button = gtk.ToggleButton("toggle button 1")# Create first button
        button.connect("toggled", self.callback, "toggle button 1")          # When the button is toggled, we call the "callback" method with a pointer to "button" as its argument
        vbox.pack_start(button, True, True, 2)
        button.show()
        
        button = gtk.ToggleButton("toggle button 2")                                 # Create second button
        button.connect("toggled", self.callback, "toggle button 2")
        vbox.pack_start(button, True, True, 2)
        button.show()
        
        vbox.show()

状态获取: 调用get_active()可以获取toggle部件(包括radio button和check button)的状态。

   def toggle_button_callback(widget, data):
      if widget.get_active():
          # If control reaches here, the toggle button is down
      else:
          # If control reaches here, the toggle button is up

状态设置: toggle_button.set_active(is_active)

is_active取值True或者False,来决定Toggle Button的状态是down或up。在创建Toggle Button时,初始默认状态为up,即false。


3, Check Button

创建: check_button = gtk.CheckButton(label=None)


实例:

        vbox = gtk.VBox(True, 2)
        self.window.add(vbox)

        # Create first button
        button = gtk.CheckButton("check button 1")
        button.connect("toggled", self.callback, "check button 1")
        vbox.pack_start(button, True, True, 2)
        button.show()

        # Create second button
        button = gtk.CheckButton("check button 2")
        button.connect("toggled", self.callback, "check button 2")
        vbox.pack_start(button, True, True, 2)
        button.show()
        
        vbox.show()


4, Radio Button

Radio Button是以一组按钮的形式出现,其中只有一个处于选中的状态。


创建: radio_button = gtk.RadioButton(group=None, label=None)

注意: 创建一组radio button中的第一个时,group=None。

实例:

        box2 = gtk.VBox(False, 10)
        box2.set_border_width(10)
        box2.show()

        button = gtk.RadioButton(None, "radio button1")
        button.connect("toggled", self.callback, "radio button 1")
        box2.pack_start(button, True, True, 0)
        button.show()

        button = gtk.RadioButton(button, "radio button2")
        button.connect("toggled", self.callback, "radio button 2")
        button.set_active(True)
        box2.pack_start(button, True, True, 0)
        button.show()




原创粉丝点击