PyGobject(三)基础篇

主循环和信号

像大多数GUI工具包,GTK +使用事件驱动的编程模型。当用户什么都不做,GTK +睡眠在主循环并等待输入。如果用户执行一些操作,比如一个鼠标点击,那么主循环“醒来”并向GTK +发送一个事件。当小部件接收事件时,他们经常发出一个或多个信号。当某些你感兴趣的事件发生时,信号就会通过你连接到该信号的方法通知你的应用程序。这种方法是通常被称为回调。当你的回调函数被调用的时候,你通常会采取一些措施,例如,当一个打开按钮被单击时你可能会显示一个文件选择器对话框。一个回调结束后,GTK +将返回到主循环并等待用户输入。
一个通用的例子是:

handler_id = widget.connect("event", callback, data)

函数原型

(int)handler_id=GObject.Object.connect(detailed_signal: str, handler: function, *args) 

第一个参数,是事件的名字,每个小部件都有自己的特定的事件发生,例如,如果你有一个按钮你通常想连接到“clicked”事件。这意味着当按钮被单击时,信号发出。第二个参数,回调函数,当事件发生时,就会调用连接的回调函数。第三个参数,调用回调函数的参数
函数返回一个数字来标识这个signal-callback信号回调。

如果要取消这个信号回调,使用下面方法

widget.disconnect(handler_id)

如果你忘记了或者某种原因丢失了这个handler_id的话,可以通过下面这个方法取消信号回调

widget.disconnect_by_func(callback)

几乎所有的应用程序都会连接到顶层窗口的“delete-event”信号,用来关闭窗口。

window.connect("delete-event", Gtk.main_quit)

Gtk.main_quit()方法将会使Gtk.main()内部的主循环退出

虚方法(Virtual Methods)

在类(继承自GObject.Object)内部,绝大多数信号都对应着一个默认执行的方法,称为虚方法。方法名为”do_信号名”,信号名中的”-“换成”_”。有些方法带参数,有的则不带
例如在上节中讲到的“delete-event”,那么它对应的信号就是do_delete_event()方法。

例子

#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/13
# section 004
TITLE = "VitualMethod"
DESCRIPTION = """
When the signal emitted,  automatic run  method
"""
import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk


class PyApp(Gtk.Window):
    def __init__(self):
        super(PyApp, self).__init__()
        self.show_all()

    def do_delete_event(self, event):
        print("do_delete_event")
        Gtk.main_quit()


def main():
    PyApp()
    Gtk.main()


if __name__ == "__main__":
    main()

当点击x关机窗口时,do_delete_event虚方法自动执行

属性

属性用来描述部件的配置和状态。每个小部件都有自己的特定的属性。例如,按钮的属性“lable”,描述按钮上的文本
创建一个标签向右对齐倾斜25度角的文本“Hello World”

label = Gtk.Label(label="Hello World", angle=25, halign=Gtk.Align.END)

等价于

label = Gtk.Label() 
label.set_label("Hello World") 
label.set_angle(25) 
label.set_halign(Gtk.Align.END)

也可以使用gobject的“props”属性,来设置或者获取部件的属性

widget.props.prop_name = value

也等价于

widget.get_property("prop-name") and widget.set_property("prop-name", value)

列出部件所有可用属性dir(widget.props)

dir(label.props)

[‘angle’, ‘app_paintable’, ‘attributes’, ‘can_default’, ‘can_focus’, ‘composite_child’, ‘cursor_position’, ‘double_buffered’, ‘ellipsize’, ‘events’, ‘expand’, ‘focus_on_click’, ‘halign’, ‘has_default’, ‘has_focus’, ‘has_tooltip’, ‘height_request’, ‘hexpand’, ‘hexpand_set’, ‘is_focus’, ‘justify’, ‘label’, ‘lines’, ‘margin’, ‘margin_bottom’, ‘margin_end’, ‘margin_left’, ‘margin_right’, ‘margin_start’, ‘margin_top’, ‘max_width_chars’, ‘mnemonic_keyval’, ‘mnemonic_widget’, ‘name’, ‘no_show_all’, ‘opacity’, ‘parent’, ‘pattern’, ‘receives_default’, ‘scale_factor’, ‘selectable’, ‘selection_bound’, ‘sensitive’, ‘single_line_mode’, ‘style’, ‘tooltip_markup’, ‘tooltip_text’, ‘track_visited_links’, ‘use_markup’, ‘use_underline’, ‘valign’, ‘vexpand’, ‘vexpand_set’, ‘visible’, ‘width_chars’, ‘width_request’, ‘window’, ‘wrap’, ‘wrap_mode’, ‘xalign’, ‘xpad’, ‘yalign’, ‘ypad’]

例子

这里写图片描述

#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/13
# section 005
TITLE = "Properties"
DESCRIPTION = """
Properties describe the configuration and state of widgets
"""
import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk


class PyApp(Gtk.Window):
    def __init__(self):
        super(PyApp, self).__init__(title="Properties")
        self.set_size_request(200, 100)
        self.label = Gtk.Label(label="Hello World", angle=25, halign=Gtk.Align.END)
        print(self.label.props.label)
        self.label.props.label = "New word"
        print(self.label.get_property("label"))
        self.label.set_property("label", "Hello World!!!")

        self.add(self.label)
        print(dir(self.label.props))


def main():
    win = PyApp()
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()


if __name__ == "__main__":
    main()




代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanxiaochengyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值