pyGTK_读书笔记_Tutorial Chapter 5&6_Widget Overview and Button Widget
2012-06-05 14:22
351 查看
在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()
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()
相关文章推荐
- 《HTML & CSS Design and Build Websites Jon DuCkeTT》读书笔记
- John McGowan's AVI Overview: Programming and Other Technical Topics
- Buttons in button bars should be borderless; use style="?android:attr/buttonBarButtonStyle" (and ?an
- <<High Performance JavaScript>>读书笔记-5.Strings and Regular Expressions
- No resource found that matches the given name 'android:Widget.Material.ActionButton'.
- <style name="Base.Widget.AppCompat.ActionButton" parent="android:Widget.Material.Acti
- < telerik:GridEditCommandColumn/> and <telerik:GridButtonColumn> imagebutton hover
- LKD 3rd 读书笔记——第1章/Overview of Operating System and Kernels
- No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse
- Drag & drop a button widget
- No resource found that matches the given name 'android:Widget.Material.ActionButton'.
- comparison between distinct pointer types 'QPushButton*' and 'QAbstractButton*' lacks a cast
- 10.10笔记,android通知栏兼容性,matches the given name 'android:Widget.Material.Button.Colored'.
- No resource found that matches the given name 'android:Widget.Material.ActionButton'
- button's UseSubmitBehavior="true" and RichText control (TelerikEditor or DevExpress Editor)
- No resource found that matches the given name 'android:Widget.Material.ActionButton'解决
- No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse
- 读书笔记:Representing and manipulation information<computer system a programmer perspective第二章>
- No resource found that matches the given name 'android:Widget.Material.ActionButton'的原因及解决方法
- No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse