PyGobject(五)布局容器之Button篇——Gtk.Button
来源:互联网 发布:日本阶级固化 知乎 编辑:程序博客网 时间:2024/06/06 18:18
- GtkButton
- 继承关系
- Methods
- static new
- static new_from_icon_name icon_name size
- static new_from_stock stock_id
- static new_with_label label
- static new_with_mnemonic label
- clicked
- enter
- get_alignment
- get_always_show_image
- get_event_window
- get_focus_on_click
- get_image
- get_image_position
- get_label
- get_relief
- get_use_stock
- get_use_underline
- leave
- pressed
- released
- set_alignment xalign yalign
- set_always_show_image always_show
- set_focus_on_click focus_on_click
- set_image image
- set_image_position position
- set_label label
- set_relief relief Parameters relief GtkReliefStyle
- set_use_stock use_stock
- set_use_underline use_underline
- Virtual Methods
- Properties
- Signals
- 例子
- 附录
- GtkPositionType
- GtkReliefStyle
在PyGobject(四)布局容器概述中讲到,布局容器有两类,单孩子容器和多孩子容器。接下来的文章我会先介绍单孩子容器,然后介绍多孩子容器。
本文主要介绍单孩子容器中的Button部件
Gtk.Button
继承关系
Methods
static new ()
Return type: Gtk.Widget
创建一个新的Gtk.Button
static new_from_icon_name (icon_name, size)
使用给定的icon name 创建一个Gtk.Button
static new_from_stock (stock_id)
使用给定的stock_id创建一个Gtk.Button
Deprecated.使用Gtk.Button.new_with_label()代替
static new_with_label (label)
使用给定的文本创建一个Gtk.Button
static new_with_mnemonic (label)
使用给定的带助记符的文本创建一个Gtk.Button。
使用下划线加字母,如”_Open”,按ALT+O可激活这个按钮
clicked ()
发送Gtk.Button ::clicked信号
enter ()
发送Gtk.Button ::enter 信号
Deprecated.使用 Gtk.Widget ::enter-notify-event 信号代替.
get_alignment ()
获取对齐方式
get_always_show_image ()
获取always-show-image属性的值
get_event_window ()
返回event window
get_focus_on_click ()
Return type: Gdk.Window
返回当点点击按钮的时候,是否获取焦点
get_image ()
获取image属性的值
get_image_position ()
获取image-position属性的值
get_label ()
获取label属性的值
get_relief ()
获取relief属性的值
get_use_stock ()
获取use-stock属性的值
get_use_underline ()
获取use-underline属性的值
leave ()
发送Gtk.Button ::leave 信号
pressed ()
发送Gtk.Button ::pressed 信号
released ()
发送Gtk.Button ::released 信号
set_alignment (xalign, yalign)
设置对齐方式
set_always_show_image (always_show)
设置是否一直显示图片
set_focus_on_click (focus_on_click)
当点点击按钮的时候,是否获取焦点。当有些时候是不需要获取焦点的,如点击toolbar,你不希望焦点离开你正在操作的区域
set_image (image)
Parameters: image(Gtk.Image)
设置图片
set_image_position (position)
Parameters: relief ( Gtk.PositionType)
设置图片位置
set_label (label)
设置文本
set_relief (relief)
Parameters: relief (Gtk.ReliefStyle)
设置浮雕样式
set_use_stock (use_stock)
设置是否是用stock id
set_use_underline (use_underline)
设置文本中的下划线是否代表助记符,如果为True,下划线表示助记符,并且不显示在button中,如果为False,助记符快捷键失效,下划线显示在文本中
Virtual Methods
Properties
Signals
例子
代码:
#!/usr/bin/env python3# Created by xiaosanyu at 16/6/14# section 006TITLE = "Button"DESCRIPTION = """The Gtk.Button widget is generally used to trigger a callback functionthat is called when the button is pressed.The various signals and how to use them are outlined below"""import gigi.require_version('Gtk', '3.0')from gi.repository import Gtkclass ButtonWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="Button Demo") self.set_border_width(10) hbox = Gtk.Box(spacing=6) self.add(hbox) button1 = Gtk.Button.new_with_label("Click Me") button1.connect("clicked", self.on_click_me_clicked) button1.connect("activate", self.on_activate) hbox.pack_start(button1, True, True, 0) # press Alt+O button = Gtk.Button.new_with_mnemonic("_Open") button.connect("clicked", self.on_open_clicked) hbox.pack_start(button, True, True, 0) # press Alt+C button = Gtk.Button.new_with_mnemonic("_Close") button.connect("clicked", self.on_close_clicked) hbox.pack_start(button, True, True, 0) # activate "Click Me" Button button = Gtk.Button(label="activate \"Click Me\" Button") # emit button1 ::activate signal button.connect("clicked", lambda *args: button1.emit("activate")) hbox.pack_start(button, True, True, 0) @staticmethod def on_click_me_clicked(button): print("\"Click me\" button was clicked") @staticmethod def on_activate(button): print("\"Click me\" button was activated") @staticmethod def on_open_clicked(button): print("\"Open\" button was clicked") def on_close_clicked(self, button): print("Closing application") self.destroy() Gtk.main_quit()def main(): win = ButtonWindow() win.connect("delete-event", Gtk.main_quit) win.show_all() Gtk.main()if __name__ == "__main__": main()
自定义一个ButtonWindow,继承自Gtk.Window,在初始化的时候,设置标题为”Button Demo”
class ButtonWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="Button Demo")
设置当前窗口与孩子部件的间距为10
self.set_border_width(10)
定义一个水平方向的多孩子容器Gtk.Box,孩子之间的间距为6,并将其放置到当前窗口
hbox = Gtk.Box(spacing=6) self.add(hbox)
使用给定的文本创建第一个button。
button1 = Gtk.Button.new_with_label("Click Me")
绑定“clicked”信号到on_click_me_clicked方法,此方法打印指定信息
button1.connect("clicked", self.on_click_me_clicked)@staticmethoddef on_click_me_clicked(button): print("\"Click me\" button was clicked")
绑定“activate”信号到on_activate方法,此方法打印指定信息
button1.connect("activate", self.on_activate)@staticmethoddef on_activate(button): print("\"Click me\" button was activated")
将这个button添加到hbox容器之中
hbox.pack_start(button1, True, True, 0)
使用助记符创建第二个button。使用下划线开头。
按下alt+下划线后面的一个字母,相当于用鼠标点击此按钮
button = Gtk.Button.new_with_mnemonic("_Open")
绑定点击事件,并添加到hbox中
button.connect("clicked", self.on_open_clicked)hbox.pack_start(button, True, True, 0)@staticmethoddef on_open_clicked(button): print("\"Open\" button was clicked")
同样使用助记符创建第三个button,并绑定点击事件。并将其添加到hbox中,点击此button,关闭窗口
button = Gtk.Button.new_with_mnemonic("_Close")button.connect("clicked", self.on_close_clicked)hbox.pack_start(button, True, True, 0)def on_close_clicked(self, button): print("Closing application") self.destroy() Gtk.main_quit()
添加第四个按钮,当点击时,”Click Me”Button发送”activate“信号
# activate "Click Me" Buttonbutton = Gtk.Button(label="activate \"Click Me\" Button")# emit button1 ::activate signalbutton.connect("clicked", lambda *args: button1.emit("activate"))hbox.pack_start(button, True, True, 0)
附录
Gtk.PositionType
class Gtk.PositionType
Bases: GObject.GEnum
图片相对于文本的位置
LEFT = 0
左边
RIGHT = 1
右边
TOP = 2
上边
BOTTOM = 3
下边
Gtk.ReliefStyle
class Gtk.ReliefStyle
Bases: GObject.GEnum
Gtk.Button边框浮雕样式
NORMAL = 0
普通浮雕
HALF = 1
半浮雕
Deprecated in 3.14
现在效果和 Gtk.ReliefStyle.NORMAL一样
NONE = 2
没有浮雕效果
代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728
- PyGobject(五)布局容器之Button篇——Gtk.Button
- PyGobject(六)布局容器之Button篇——Gtk.ColorButton
- PyGobject(七)布局容器之Button篇——Gtk.FontButton
- PyGobject(八)布局容器之Button篇——Gtk.LinkButton
- PyGobject(九)布局容器之Button篇——Gtk.ToggleButton
- PyGobject(十)布局容器之Button篇——Gtk.CheckButton
- PyGobject(十一)布局容器之Button篇——Gtk.RadioButton
- PyGobject(十二)布局容器之Button篇——Gtk.MenuButton
- PyGobject(十四)布局容器之Button篇——Gtk.LockButton
- PyGobject(十五)布局容器之Button篇——Gtk.ScaleButton
- PyGobject(十六)布局容器之Button篇——Gtk.VolumeButton
- PyGobject(十七)布局容器之Button篇——Gtk.ModelButton
- PyGobject(十三)布局容器之Button篇——图片按钮和圆形按钮
- PyGobject(十八)布局容器之ActionBar
- PyGobject(十九)布局容器之Alignment
- PyGobject(三十)布局容器之StackSidebar
- PyGobject(三十一)布局容器之EventBox
- PyGobject(三十二)布局容器之Window
- 使用ajaxfileupload上传文件
- Windows下使用krpanotools命令行生成全景图
- VS 2015 OpenCV环境搭建
- Spring 消息本地化实现(使用注释实现)/使用自定义消息
- LoadRunner监控Tomcat性能以及tomcat环境部署
- PyGobject(五)布局容器之Button篇——Gtk.Button
- 【9004】&&【7010】最优布线问题
- 配置型软件开发平台
- 一起学Netty(十九)netty源码学习之netty server端源码初读(下)
- 【代码库】删除select元素节点下所有的OPTION节点
- iOS 改变状态栏的字体颜色
- selenium处理页面等待时间的几种方式
- 12864oled模块
- 【Codeforces Round 363 (Div 2) A】【水题】Launch of Collider 机器人最早碰撞时间