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 () 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) set_use_stock (use_stock) set_use_underline (use_underline)

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

do_activate () do_clicked () do_enter () do_leave () do_pressed () do_released ()

Properties

Name Type Flags Short Description always-show-image bool r/w/c/en 图片是否一直显示 image Gtk.Image r/w/en 图片 image-position Gtk.PositionType r/w/en 图片相对于文本的位置 label str r/w/c/en 文本内容 relief Gtk.ReliefStyle r/w/en 边框浮雕样式 use-stock bool d/r/w/c/en 如果设置,则button显示stock id 表示的文本 deprecated use-underline bool r/w/c/en 是否开启助记符 xalign float d/r/w/en 0.0左对齐,1.0右对齐 deprecated yalign float d/r/w/en 0.0上对齐, 1.0 下对齐 deprecated

Signals

Name Short Description activate 当按钮发送”activate”信号时,对应的方法执行。并且“clicked”信号对应的方法也执行(见例子). clicked 当按钮被激活发出(按下并释放). enter 当指针进入按钮时发出. deprecated leave 当指针离开按钮时发出. deprecated pressed 当按钮按下时发出. deprecated released 当指按钮释放时发出. deprecated

例子

这里写图片描述
代码:

#!/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

0 0