Re: [pygtk] self from gtk.builder

2010-12-05 Thread Marco Giusti
On Sun, Dec 05, 2010 at 08:54:28PM +, Leon Bogaert wrote:
> Hi all,
> 
> Is it possible to do something like this?
> 
> class ActionsMenu(gtk.Menu):
> def __init__(self):
> builder = gtk.Builder()
> builder.add_from_file("bctimer.xml") 
> 
> self = builder.get_object("menuActions")

yes but it doesn't do what you think: it rebinds the name `self` to the
builder object but the other references point to the `ActionsMenu`.

what you can do is something like the following:

class BaseWindow(object):

def __init__(self, builder, auto_connect=True):
self.__builder = builder
if auto_connect:
builder.connect_signals(self)

def __getattr__(self, n):
w = self.__builder.get_object(n)
if w is not None:
return w
raise AttributeError, n

or even, but not tested:

...
def __getattr__(self, n):
w = self.__builder.get_object(n)
if w is not None:
setattr(self, n, w) # here the point
return w
raise AttributeError, n

m.


-- 
Excellentium virorum est improborum negligere contumeliam,
a quibus etiam laudari turpe.
-- Plutarco
___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/


[pygtk] [OT] Re: dynamic treestore

2010-11-28 Thread Marco Giusti
On Sun, Nov 28, 2010 at 01:53:21AM -0600, alex goretoy wrote:
> wow, thank you. This is exactly what I was looking for :)
> 
> def get_treestore(n=1):
> if n:
> return gtk.TreeStore(*((str,) * n))
> return gtk.TreeStore(str)

usually I prefer do not check for function parameters.
who is calling `get_treestore`? if the input come from the user I prefer
check the input and then call, or not, the function. if the input come
from a computation, a wrong value is a bug in the program that need to
be fixed. just if I want to be sure I add an `assert` statement.

m.

-- 
Excellentium virorum est improborum negligere contumeliam,
a quibus etiam laudari turpe.
-- Plutarco
___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/


Re: [pygtk] dynamic treestore

2010-11-27 Thread Marco Giusti
On Sat, Nov 27, 2010 at 08:40:46PM -0600, alex goretoy wrote:
> Currently I am having to do this in my application to create dynamic
> treestore; is the a better way to do this?
> 
> def get_treestore(n):
> if n == 0:
> treestore = gtk.TreeStore(str);
> elif n == 1:
> treestore = gtk.TreeStore(str);
> elif n == 2:
> treestore = gtk.TreeStore(str, str);
> elif n == 3:
> treestore = gtk.TreeStore(str, str, str);
> elif n == 4:
> treestore = gtk.TreeStore(str, str, str, str);
> else:
> treestore = gtk.TreeStore(str);
> 
> return treestore

shorter but not prettier:

def get_treestore(n):
if n in range(1, 5):
return gtk.TreeStore(*((str,) * n))
return gtk.TreeStore(str)

but you could make some assertions about the parameter passed to
`get_treestore`, for example you could assert that `n` has always the
right value. in that case the function became:

def get_treestore(n=1):
return gtk.TreeStore(*((str,) * n))

m.


-- 
Lo punite del fatto che la sua infanzia ha strisciato sul suolo senza
stelo e senza tutore; gli imputate come un misfatto l'isolamento in cui
lo avete lasciato; della sua sventura fate il suo delitto! Nessuno gli
ha insegnato a sapere ciĆ² che faceva: quest'uomo ignora. La sua colpa
appartiene al suo destino, non a lui. Voi colpite un innocente.
-- Victor Hugo
___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/