PyGobject(三)基础篇

来源:互联网 发布:淘宝浏览器 编辑:程序博客网 时间:2024/06/16 01:20

  • 主循环和信号
  • 虚方法Virtual Methods
    • 例子
  • 属性
    • 例子

主循环和信号

像大多数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 004TITLE = "VitualMethod"DESCRIPTION = """When the signal emitted,  automatic run  method"""import gigi.require_version("Gtk", "3.0")from gi.repository import Gtkclass 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 005TITLE = "Properties"DESCRIPTION = """Properties describe the configuration and state of widgets"""import gigi.require_version("Gtk", "3.0")from gi.repository import Gtkclass 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

0 0
原创粉丝点击