Hi,
    Last two days I was studying Sub-classing GObject.
Can any one given a pointer, why 
raise AttributeError, "Unknown property %s" % property.name
is not raising, when I tried to set wrong properties?

---- Glade file: valueSelection.glade  starts here ----

<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd";>

<glade-interface>

<widget class="GtkDialog" id="dlgValueSelection">
  <property name="width_request">350</property>
  <property name="height_request">250</property>
  <property name="title" translatable="yes">Choose Value</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>
  <property name="decorated">True</property>
  <property name="skip_taskbar_hint">False</property>
  <property name="skip_pager_hint">False</property>
  <property name="type_hint">GDK_WINDOW_TYPE_HINT_DIALOG</property>
  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  <property name="has_separator">True</property>

  <child internal-child="vbox">
    <widget class="GtkVBox" id="dialog-vbox1">
      <property name="visible">True</property>
      <property name="homogeneous">False</property>
      <property name="spacing">0</property>

      <child internal-child="action_area">
        <widget class="GtkHButtonBox" id="dialog-action_area1">
          <property name="visible">True</property>
          <property name="layout_style">GTK_BUTTONBOX_END</property>

          <child>
            <widget class="GtkButton" id="btnNew">
              <property name="visible">True</property>
              <property name="label">gtk-new</property>
              <property name="use_stock">True</property>
              <property name="relief">GTK_RELIEF_NORMAL</property>
              <property name="focus_on_click">False</property>
              <property name="response_id">-2</property>
            </widget>
          </child>

          <child>
            <widget class="GtkButton" id="btnCancel">
              <property name="visible">True</property>
              <property name="label">gtk-cancel</property>
              <property name="use_stock">True</property>
              <property name="relief">GTK_RELIEF_NORMAL</property>
              <property name="focus_on_click">False</property>
              <property name="response_id">-6</property>
            </widget>
          </child>

          <child>
            <widget class="GtkButton" id="btnOk">
              <property name="visible">True</property>
              <property name="can_default">True</property>
              <property name="has_default">True</property>
              <property name="can_focus">True</property>
              <property name="label">gtk-ok</property>
              <property name="use_stock">True</property>
              <property name="relief">GTK_RELIEF_NORMAL</property>
              <property name="focus_on_click">False</property>
              <property name="response_id">-5</property>
            </widget>
          </child>
        </widget>
        <packing>
          <property name="padding">0</property>
          <property name="expand">False</property>
          <property name="fill">True</property>
          <property name="pack_type">GTK_PACK_END</property>
        </packing>
      </child>

      <child>
        <widget class="GtkScrolledWindow" id="scrScroll">
          <property name="border_width">4</property>
          <property name="visible">True</property>
          <property name="hscrollbar_policy">GTK_POLICY_AUTOMATIC</property>
          <property name="vscrollbar_policy">GTK_POLICY_AUTOMATIC</property>
          <property name="shadow_type">GTK_SHADOW_IN</property>
          <property name="window_placement">GTK_CORNER_TOP_LEFT</property>

          <child>
            <widget class="GtkTreeView" id="trvList">
              <property name="visible">True</property>
              <property name="headers_visible">True</property>
              <property name="rules_hint">True</property>
              <property name="reorderable">False</property>
              <property name="enable_search">True</property>
            </widget>
          </child>
        </widget>
        <packing>
          <property name="padding">0</property>
          <property name="expand">True</property>
          <property name="fill">True</property>
        </packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>


---- Glade file: valueSelection.glade  ends here ----


---- Python file: valueSelection.py  starts here ----

#!/usr/bin/env python

"""ValueSelectionDialog widget, for easy choosing of values from a list."""

__all__ = ["EmptyError", "ValueSelectionDialog"]
__version__ = "0.1.0"

import sys
import os

import pygtk
pygtk.require("2.0")
import gobject

import gtk
import gtk.glade

class EmptyError(Exception):
    """Exception raised when the data in model is empty.
    
    >>> raise EmptyError, "The model contains no data"
    Traceback (most recent call last):
        ...
    EmptyError: The model contains no data
    """
    
    def __init__(self, msg):
        self.msg = msg
        Exception.__init__(self, msg, "")
        pass
    
    def __str__(self):
        return self.msg
    pass

class ValueSelectionDialog(gobject.GObject):
    """Implement ValueSelectionDialog
    
    >>> ValueSelectionDialog().column_left_name
    'Code'
    >>> ValueSelectionDialog().column_right_name
    'Name'
    """
    
    __gproperties__ = {
        "model" : (gobject.TYPE_OBJECT, "The model the widget",
                   "The model to set to the TreeView in ValueSelectionDialog",
                   gobject.PARAM_READWRITE),
        "column-left" : (gobject.TYPE_INT, "The left column of model",
                         "The left column of model to display in TreeView",
                         0, 100, 1, gobject.PARAM_READWRITE),
        "column-right" : (gobject.TYPE_INT, "The right column of model",
                          "The right column of model to display in TreeView",
                          0, 100, 1, gobject.PARAM_READWRITE),
        "column-left-name" : (gobject.TYPE_STRING, "The name of left
column of model",
                              "The name of left column of model to
display in TreeView",
                              "Code", gobject.PARAM_READWRITE),
        "column-right-name" : (gobject.TYPE_STRING, "The name of right
column of model",
                               "The name of right column of model to
display in TreeView",
                               "Name", gobject.PARAM_READWRITE),
        "sensitive-new":  (gobject.TYPE_BOOLEAN, "If true new button's
sensitive is true",
                           "If true new button's sensitive is true",
                           gtk.TRUE, gobject.PARAM_READWRITE)
        }
    
    __gsignals__ = {
        "new-clicked": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                        (gobject.TYPE_STRING,)),
        "cancel-clicked": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                           (gobject.TYPE_STRING,)),
        "ok-clicked": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                       (gobject.TYPE_STRING,))
        }
    
    
    def __init__(self, model=None, column_left=1,  column_right=2,
column_left_name="Code", column_right_name="Name"):
        gobject.GObject.__init__(self)
        
        self.model = model
        self.column_left = column_left
        self.column_right = column_right
        self.column_left_name = column_left_name
        self.column_right_name = column_right_name
        
        self.xml = gtk.glade.XML("valueSelection.glade")
        
        self.topValueSelection = self.xml.get_widget("dlgValueSelection")
        
        self.trvList = self.xml.get_widget("trvList")
        self.btnNew = self.xml.get_widget("btnNew")
        self.btnCancel = self.xml.get_widget("btnCancel")
        self.btnOk = self.xml.get_widget("btnOk")
        
        self.btnNew.connect("clicked", self.new_clicked)
        self.btnCancel.connect("clicked", self.cancel_clicked)
        self.btnOk.connect("clicked", self.ok_clicked)
        
        self.trvList.set_model(model)
        self.trvList.get_selection().connect("changed", self.list_changed)
        self.trvList.connect("row_activated", self.list_activated)
        
        self.cell_left = gtk.CellRendererText()
        self.left_col = gtk.TreeViewColumn(column_left_name, self.cell_left)
        self.left_col.set_attributes(self.cell_left, text=column_left)
        self.left_col.set_sort_column_id(column_left)
        self.trvList.append_column(self.left_col)
        
        self.cell_right = gtk.CellRendererText()
        self.right_col = gtk.TreeViewColumn(column_right_name, self.cell_right)
        self.right_col.set_attributes(self.cell_right, text=column_right)
        self.right_col.set_sort_column_id(column_right)
        self.trvList.append_column(self.right_col)
        
        self.trvList.set_search_column(column_left)
        pass
    
    def do_set_property(self, property, value):
        if property.name == "model":
            self.model = value
        elif property.name == "column-left":
            self.column_left = value
            self.left_col.set_attributes(self.cell_left, text=value)
            self.left_col.set_sort_column_id(value)
        elif property.name == "column-right":
            self.column_right = value
            self.right_col.set_attributes(self.cell_right, text=value)
            self.right_col.set_sort_column_id(value)
        elif property.name == "column-left-name":
            self.column_left_name = value
            self.left_col.set_title(value)
        elif property.name == "column-right-name":
            self.column_right_name = value
            self.right_col.set_title(value)
        elif property.name == "sensitive-new":
            self.sensitive_new = value
            self.btnNew.set_sensitive(value)
        else:
            ##FIXME: This never raise error? -- baiju
            raise AttributeError, "Unknown property %s" % property.name
        pass
    
    def show(self):
        self.topValueSelection.show_all()
        pass
    
    def hide(self):
        self.topValueSelection.hide()
        return gtk.TRUE
    
    def list_changed(self, *args):
        if self.trvList.get_selection().get_selected()[1]:
            if self.trvList.get_cursor()[1]:
                column = self.trvList.get_cursor()[1].get_sort_column_id()
                self.trvList.set_search_column(column)
                pass
            pass
        pass
    
    def list_activated(self, *args):
        self.btnOk.grab_focus()
        pass
    
    def new_clicked(self, *args):
        print "new-clicked"
        self.emit('new_clicked', self.get_property('model'))
        pass
    
    def cancel_clicked(self, *args):
        print "cancel_clicked"
        self.emit('cancel_clicked', self.get_property('model'))
        pass
    
    def ok_clicked(self, *args):
        print "ok_clicked"
        self.emit('ok_clicked', self.get_property('model'))
        pass
    
    pass


gobject.type_register(ValueSelectionDialog)

##Testing starts here
def test_function(*args):
    print "test_function called", args
    gtk.Dialog().show()
    #c.hide()
    pass

def _test():
    import doctest, valueSelection
    return doctest.testmod(valueSelection)

if __name__ == "__main__":
    _test()
    sto_data = gtk.ListStore(gobject.TYPE_INT64,
                             gobject.TYPE_STRING,
                             gobject.TYPE_STRING)
    for i in "qwertyuiopasdfghjklzxcvbnm":
        itr = sto_data.append()
        sto_data.set(itr,
                     1, i,
                     2, "a"+i+"Test 1")
        pass
    
##     c = ValueSelectionDialog(sto_data)
##     c.set_property("model", sto_data)
##     c.set_property("column_left", 2)
##     c.set_property("column_right", 1)
##     c.set_property("column_left_name", "Cod_e")
##     c.set_property("column_right_name", "Na_me")
##     c.set_property("sensitive_new", gtk.TRUE)
    c = ValueSelectionDialog(sto_data, 2, 1, "Code", "Name")
    c.set_property("sensitive_new", gtk.FALSE)
    c.connect("new_clicked", test_function)
    c.connect("cancel_clicked", test_function)
    c.connect("ok_clicked", test_function)
    c.show()
    gtk.main()
    pass

---- Python file: valueSelection.py  starts here ----
_______________________________________________
pygtk mailing list   [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to