天天看點

PyGobject(三)基礎篇主循環和信号虛方法(Virtual Methods)屬性

  • 主循環和信号
  • 虛方法Virtual Methods
    • 例子
  • 屬性
    • 例子

主循環和信号

像大多數GUI工具包,GTK +使用事件驅動的程式設計模型。當使用者什麼都不做,GTK +睡眠在主循環并等待輸入。如果使用者執行一些操作,比如一個滑鼠點選,那麼主循環“醒來”并向GTK +發送一個事件。當小部件接收事件時,他們經常發出一個或多個信号。當某些你感興趣的事件發生時,信号就會通過你連接配接到該信号的方法通知你的應用程式。這種方法是通常被稱為回調。當你的回調函數被調用的時候,你通常會采取一些措施,例如,當一個打開按鈕被單擊時你可能會顯示一個檔案選擇器對話框。一個回調結束後,GTK +将傳回到主循環并等待使用者輸入。

一個通用的例子是:

函數原型

(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=, halign=Gtk.Align.END)
           

等價于

label = Gtk.Label() 
label.set_label("Hello World") 
label.set_angle() 
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)

[‘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’]

例子

PyGobject(三)基礎篇主循環和信号虛方法(Virtual Methods)屬性
#!/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(, )
        self.label = Gtk.Label(label="Hello World", angle=, 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