Re: [pygtk] Liststore model

2007-01-14 Thread Florian Diesch
Seth Mahoney <[EMAIL PROTECTED]> wrote:

> If this doesn't work, then I've got another idea (though I don't know if
> it will work for you, either).  You could try generating the liststore
> with a str for every possible column, and then only show the columns you
> need.  So, if there were five different data items:
>
> store = gtk.ListStore(str, str, str, str, str)
>
> and then use gtk.TreeView.append_column() and
> gtk.TreeView.remove_column() to hide and show columns as necessary.

Another way is to use a ListStore with a column containing complex data
like doicts, lists or objects and then use a cell_data_func to access
its attributes. Like this (extracted from some of my code, maybeneeds
some fixing, but the idea should be clear):

class Foo(object):
 def __init__(self, bar):
self.bar=bar

self.model=gtk.ListStore(gobject.TYPE_PYOBJECT)
self.model.append([Foo(i) for i in range(0,5)])
self.widget.set_model(self.model)

cell=gtk.CellRendererText()
col=gtk.TreeViewColumn(self.title)
col.pack_start(cell, True)
def callback(column, cell, model, iter):
   cell.set_property('text', 
 getattr(model.get_value(iter, 0), 'bar'))
col.set_cell_data_func(cell, callback)
self.widget.append_column(col)




   Florian
-- 

___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/


Re: [pygtk] Liststore model

2007-01-13 Thread Richard Taylor
Another option is to wrap the return in a class instance an put that in the 
ListStore.


store = gtk.ListStore(object)

Then write a gtk.TreeView that takes the cur.description and build the correct 
columns. You can use TreeViewColumn.set_cell_data_func to set an method that 
gets called to fill in the column. 

Here is a small part of the code from Gramps that does this, although it does 
not calculate anything dynamically you should able to see how to do it:

class PersonTreeView(gtk.TreeView):

def __init__(self,db,apply_filter=None):
gtk.TreeView.__init__(self)

self._db = db

# Add the Name column
cols = (\
(_("Name"),300,self._family_name),\
(_("ID"),100,self._object_id),\
(_("Gender"),100,self._gender),\
(_("Birth Date"),200,self._birth_date),\
(_("Birth Place"),200,self._birth_place),\
(_("Death Date"),200,self._death_date),\
(_("Death Place"),200,self._death_place),\
(_("Spouse"),200,self._spouce),\
(_("Last Change"),200,self._last_change),\
(_("Cause of Death"),300,self._death_cause))

for col in cols:
self.append_column(
self._new_column(
col[0],col[1],col[2]))

self.set_enable_search(False)
self.set_fixed_height_mode(True)

def _new_column(self,name,size,func):
col = gtk.TreeViewColumn(name)
col.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
col.set_fixed_width(size)
cell = gtk.CellRendererText()
col.pack_start(cell,True)
col.set_cell_data_func(cell,func)
return col

If you replaces cols with something generated from the cur.description 
information you would be half way there.

Richard

On Saturday 13 January 2007 02:30, Seth Mahoney wrote:
> If this doesn't work, then I've got another idea (though I don't know if
> it will work for you, either).  You could try generating the liststore
> with a str for every possible column, and then only show the columns you
> need.  So, if there were five different data items:
>
> store = gtk.ListStore(str, str, str, str, str)
>
> and then use gtk.TreeView.append_column() and
> gtk.TreeView.remove_column() to hide and show columns as necessary.
>
> OR (and I'm not sure this will work for you either), just have one
> column, and create its data on the fly by joining strings.  So:
>
> store = gtk.ListStore(str)
> store.append([data1 + " " + data2 + " " + data 3 ...)
>
> On Fri, 2007-01-12 at 09:52 -0500, Chris Lambacher wrote:
> > On Fri, Jan 12, 2007 at 03:32:05PM +0100, Volker Helm wrote:
> > > Hi Jarek,
> > >
> > > > > 1. A select is send to a database:
> > > > > cur.execute("select position,article,name,amount,value from
> > > >
> > > > table_bill_pos")
> > > >
> > > > > pos = cur.fetchall()
> > > > >
> > > > > 2. depending on the result, I generate the model:
> > > > > listmodel = gtk.ListStore(int,str,str,str,str)
> >
> > columns = [int, str, str, int]
> > listmodel = gtk.ListStore(*columns)
> > columns.append(str)
> > listmodel = gtk.ListStore(*columns)
> >
> >
> > see http://docs.python.org/tut/node6.html#SECTION00674
> >
> > > > > 3. create the TreeView:
> > > > > tv = gtk.TreeView(listmodel)
> > > > >
> > > > > My problem is the second part, since I've got many queries against
> > > > > the
> > > >
> > > > database and changing the queries quite often.
> > > >
> > > > > So, I want to generate the _listmodel_ dynamically. Next time,
> > > > > maybe the
> > > >
> > > > query could be something like (int,str,str,str,str,str,int), because
> > > > I added two  columns.
> > > >
> > > > Cursor objects have description. It's a tuple, where second element
> > > > is typecode. You can build your ListStore using that information.
> > >
> > > that sound great, but I'm blocked. I using cur.description to get the
> > > information of each column (type and header). But I don't know a way to
> > > generate the gtk.ListStore() dynamically.
> >
> > See my comments above
> >
> > > Can you give me some code that shows the way of doing it. I tried to
> > > generate a basic gtk.ListStore(int) model, then appending further
> > > columns, but I could get it work.
> > >
> > > If I solve this problem, I would make a little tutorial about this,
> > > maybe someone has similar problems.
> > >
> > > Thanks you,
> >
> > Your welcome,
> > Chris
> > ___
> > pygtk mailing list   pygtk@daa.com.au
> > http://www.daa.com.au/mailman/listinfo/pygtk
> > Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
>
> ___
> pygtk mailing list   pygtk@daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http:

Re: Re: [pygtk] Liststore model

2007-01-12 Thread Seth Mahoney
If this doesn't work, then I've got another idea (though I don't know if
it will work for you, either).  You could try generating the liststore
with a str for every possible column, and then only show the columns you
need.  So, if there were five different data items:

store = gtk.ListStore(str, str, str, str, str)

and then use gtk.TreeView.append_column() and
gtk.TreeView.remove_column() to hide and show columns as necessary.

OR (and I'm not sure this will work for you either), just have one
column, and create its data on the fly by joining strings.  So:

store = gtk.ListStore(str)
store.append([data1 + " " + data2 + " " + data 3 ...)

On Fri, 2007-01-12 at 09:52 -0500, Chris Lambacher wrote:
> On Fri, Jan 12, 2007 at 03:32:05PM +0100, Volker Helm wrote:
> > Hi Jarek,
> > 
> > > > 1. A select is send to a database:
> > > > cur.execute("select position,article,name,amount,value from
> > > table_bill_pos")
> > > > pos = cur.fetchall()
> > > > 
> > > > 2. depending on the result, I generate the model:
> > > > listmodel = gtk.ListStore(int,str,str,str,str)
> columns = [int, str, str, int]
> listmodel = gtk.ListStore(*columns)
> columns.append(str)
> listmodel = gtk.ListStore(*columns)
> 
> 
> see http://docs.python.org/tut/node6.html#SECTION00674
> 
> > > > 
> > > > 3. create the TreeView:
> > > > tv = gtk.TreeView(listmodel)
> > > > 
> > > > My problem is the second part, since I've got many queries against the
> > > database and changing the queries quite often. 
> > > > So, I want to generate the _listmodel_ dynamically. Next time, maybe the
> > > query could be something like (int,str,str,str,str,str,int), because I
> > > added two  columns.
> > > 
> > > Cursor objects have description. It's a tuple, where second element is
> > > typecode. You can build your ListStore using that information.
> > 
> > that sound great, but I'm blocked. I using cur.description to get the 
> > information of each column (type and header). But I don't know a way to 
> > generate the gtk.ListStore() dynamically.
> See my comments above
> > 
> > Can you give me some code that shows the way of doing it. I tried to 
> > generate a basic gtk.ListStore(int) model, then appending further columns, 
> > but I could get it work. 
> > 
> > If I solve this problem, I would make a little tutorial about this, maybe 
> > someone has similar problems.
> > 
> > Thanks you,
> 
> Your welcome,
> Chris
> ___
> pygtk mailing list   pygtk@daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
> 

___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/


Re: Re: [pygtk] Liststore model

2007-01-12 Thread Volker Helm
Hi Chris,

I going to make this work next week. Thanks for your help, I never used the  
*-operator before, I think I'll change this.

Have a nice weekend,

Volker 

 Original-Nachricht 
Datum: Fri, 12 Jan 2007 09:52:10 -0500
Von: Chris Lambacher <[EMAIL PROTECTED]>
An: Volker Helm <[EMAIL PROTECTED]>
Betreff: Re: Re: [pygtk] Liststore model

> On Fri, Jan 12, 2007 at 03:32:05PM +0100, Volker Helm wrote:
> > Hi Jarek,
> > 
> > > > 1. A select is send to a database:
> > > > cur.execute("select position,article,name,amount,value from
> > > table_bill_pos")
> > > > pos = cur.fetchall()
> > > > 
> > > > 2. depending on the result, I generate the model:
> > > > listmodel = gtk.ListStore(int,str,str,str,str)
> columns = [int, str, str, int]
> listmodel = gtk.ListStore(*columns)
> columns.append(str)
> listmodel = gtk.ListStore(*columns)
> 
> 
> see http://docs.python.org/tut/node6.html#SECTION00674
> 
> > > > 
> > > > 3. create the TreeView:
> > > > tv = gtk.TreeView(listmodel)
> > > > 
> > > > My problem is the second part, since I've got many queries against
> the
> > > database and changing the queries quite often. 
> > > > So, I want to generate the _listmodel_ dynamically. Next time, maybe
> the
> > > query could be something like (int,str,str,str,str,str,int), because I
> > > added two  columns.
> > > 
> > > Cursor objects have description. It's a tuple, where second element is
> > > typecode. You can build your ListStore using that information.
> > 
> > that sound great, but I'm blocked. I using cur.description to get the
> information of each column (type and header). But I don't know a way to
> generate the gtk.ListStore() dynamically.
> See my comments above
> > 
> > Can you give me some code that shows the way of doing it. I tried to
> generate a basic gtk.ListStore(int) model, then appending further columns, but
> I could get it work. 
> > 
> > If I solve this problem, I would make a little tutorial about this,
> maybe someone has similar problems.
> > 
> > Thanks you,
> 
> Your welcome,
> Chris

-- 
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! 
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/


Re: Re: [pygtk] Liststore model

2007-01-12 Thread Chris Lambacher
On Fri, Jan 12, 2007 at 03:32:05PM +0100, Volker Helm wrote:
> Hi Jarek,
> 
> > > 1. A select is send to a database:
> > > cur.execute("select position,article,name,amount,value from
> > table_bill_pos")
> > > pos = cur.fetchall()
> > > 
> > > 2. depending on the result, I generate the model:
> > > listmodel = gtk.ListStore(int,str,str,str,str)
columns = [int, str, str, int]
listmodel = gtk.ListStore(*columns)
columns.append(str)
listmodel = gtk.ListStore(*columns)


see http://docs.python.org/tut/node6.html#SECTION00674

> > > 
> > > 3. create the TreeView:
> > > tv = gtk.TreeView(listmodel)
> > > 
> > > My problem is the second part, since I've got many queries against the
> > database and changing the queries quite often. 
> > > So, I want to generate the _listmodel_ dynamically. Next time, maybe the
> > query could be something like (int,str,str,str,str,str,int), because I
> > added two  columns.
> > 
> > Cursor objects have description. It's a tuple, where second element is
> > typecode. You can build your ListStore using that information.
> 
> that sound great, but I'm blocked. I using cur.description to get the 
> information of each column (type and header). But I don't know a way to 
> generate the gtk.ListStore() dynamically.
See my comments above
> 
> Can you give me some code that shows the way of doing it. I tried to generate 
> a basic gtk.ListStore(int) model, then appending further columns, but I could 
> get it work. 
> 
> If I solve this problem, I would make a little tutorial about this, maybe 
> someone has similar problems.
> 
> Thanks you,

Your welcome,
Chris
___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/


Re: Re: [pygtk] Liststore model

2007-01-12 Thread Volker Helm
Hi Jarek,

> > 1. A select is send to a database:
> > cur.execute("select position,article,name,amount,value from
> table_bill_pos")
> > pos = cur.fetchall()
> > 
> > 2. depending on the result, I generate the model:
> > listmodel = gtk.ListStore(int,str,str,str,str)
> > 
> > 3. create the TreeView:
> > tv = gtk.TreeView(listmodel)
> > 
> > My problem is the second part, since I've got many queries against the
> database and changing the queries quite often. 
> > So, I want to generate the _listmodel_ dynamically. Next time, maybe the
> query could be something like (int,str,str,str,str,str,int), because I
> added two  columns.
> 
> Cursor objects have description. It's a tuple, where second element is
> typecode. You can build your ListStore using that information.

that sound great, but I'm blocked. I using cur.description to get the 
information of each column (type and header). But I don't know a way to 
generate the gtk.ListStore() dynamically.

Can you give me some code that shows the way of doing it. I tried to generate a 
basic gtk.ListStore(int) model, then appending further columns, but I could get 
it work. 

If I solve this problem, I would make a little tutorial about this, maybe 
someone has similar problems.

Thanks you,


-- 
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! 
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/


Re: [pygtk] Liststore model

2007-01-12 Thread Volker Helm
Hi Steve.

This was my intention, because the query against the database is fixed, maybe 
modified. But the result is defined for each column.

So, first a query against the database, result a the definition of the columns 
including the types and the names of each column. cur.description() is really 
nice ;).
Second I would generate the model, but I couldn't find a way to do dynamically. 
This would be an object the generate the treeview dynamically, so the header, 
the types of gtk.ListStore()  will be defined.

Hope I made myself clear,

Volker

 Original-Nachricht 
Datum: Fri, 12 Jan 2007 07:56:21 -0500
Von: "Steve McClure" <[EMAIL PROTECTED]>
An: Volker Helm <[EMAIL PROTECTED]>
Betreff: Re: [pygtk] Liststore model

> On Fri, 2007-01-12 at 13:45 +0100, Volker Helm wrote:
> > Hi,
> > 
> > I'm trying to implement programm based on a database. Since there are a
> lot of columns in a database I'd like to generate a gtk.ListStore model
> dynamicly.
> > 
> > normally, I'd do something like this:
> > 
> > import pygtk
> > ...
> > win = gtk.Window()
> > listmodel = gtk.ListStore(str,str,str,int,str)
> > tv = gtk.TreeView(listmodel)
> > ...
> > 
> > I'd prefer:
> > 
> > import gtk
> > ...
> > win = gtk.Window()
> > listmodel = gtk.Liststore(str)
> > # now append some column to the model
> > 
> > # go on
> > tv = gtk.Treeview(listmodel)
> > 
> > Does anybody has an idea, how to solve this problem without using
> completly the generic model?
> > 
> > Thanks in advance,
> > 
> > Volker
> 
> Can you fetch your schema before creating the ListStore and then,
> knowing the schema at that point, pass the correct types to it?
> -- 
> Steve McClure   Racemi
> email: [EMAIL PROTECTED]  380 Interstate North Pkwy,
> SE
> voice: 404-892-5850 Suite 250
> fax: 404-892-7215   Atlanta, GA 30339
> http://www.racemi.com
> 
> ___
> pygtk mailing list   pygtk@daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

-- 
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! 
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/


Re: Re: [pygtk] Liststore model

2007-01-12 Thread Jarek Zgoda
> 1. A select is send to a database:
> cur.execute("select position,article,name,amount,value from table_bill_pos")
> pos = cur.fetchall()
> 
> 2. depending on the result, I generate the model:
> listmodel = gtk.ListStore(int,str,str,str,str)
> 
> 3. create the TreeView:
> tv = gtk.TreeView(listmodel)
> 
> My problem is the second part, since I've got many queries against the 
> database and changing the queries quite often. 
> So, I want to generate the _listmodel_ dynamically. Next time, maybe the 
> query could be something like (int,str,str,str,str,str,int), because I added 
> two  columns.

Cursor objects have description. It's a tuple, where second element is 
typecode. You can build your ListStore using that information.

Cheers
Jarek Zgoda
___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/


Re: [pygtk] Liststore model

2007-01-12 Thread Volker Helm
Sorry for my bad english, I rarely use it.

For understanding. 

1. A select is send to a database:
cur.execute("select position,article,name,amount,value from table_bill_pos")
pos = cur.fetchall()

2. depending on the result, I generate the model:
listmodel = gtk.ListStore(int,str,str,str,str)

3. create the TreeView:
tv = gtk.TreeView(listmodel)

My problem is the second part, since I've got many queries against the database 
and changing the queries quite often. 
So, I want to generate the _listmodel_ dynamically. Next time, maybe the query 
could be something like (int,str,str,str,str,str,int), because I added two  
columns.

I hope, this is understandable.

Thanks in advance



 Original-Nachricht 
Datum: Fri, 12 Jan 2007 12:50:29 +
Von: "Alberto Ruiz" <[EMAIL PROTECTED]>
An: "Volker Helm" <[EMAIL PROTECTED]>
Betreff: Re: [pygtk] Liststore model

> 2007/1/12, Volker Helm <[EMAIL PROTECTED]>:
> >
> > Hi,
> >
> > I'm trying to implement programm based on a database. Since there are a
> > lot of columns in a database I'd like to generate a gtk.ListStore model
> > dynamicly.
> >
> > normally, I'd do something like this:
> >
> > import pygtk
> > ...
> > win = gtk.Window()
> > listmodel = gtk.ListStore(str,str,str,int,str)
> > tv = gtk.TreeView(listmodel)
> > ...
> >
> > I'd prefer:
> >
> > import gtk
> > ...
> > win = gtk.Window()
> > listmodel = gtk.Liststore(str)
> > # now append some column to the model
> >
> > # go on
> > tv = gtk.Treeview(listmodel)
> >
> > Does anybody has an idea, how to solve this problem without using
> > completly the generic model?
> >
> > Thanks in advance,
> 
> 
> Sorry, but I don't get what the question is.
> 
> Volker
> >
> 
> -- 
> Un saludo,
> Alberto Ruiz

-- 
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! 
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/


Re: [pygtk] Liststore model

2007-01-12 Thread Steve McClure
On Fri, 2007-01-12 at 13:45 +0100, Volker Helm wrote:
> Hi,
> 
> I'm trying to implement programm based on a database. Since there are a lot 
> of columns in a database I'd like to generate a gtk.ListStore model dynamicly.
> 
> normally, I'd do something like this:
> 
> import pygtk
> ...
> win = gtk.Window()
> listmodel = gtk.ListStore(str,str,str,int,str)
> tv = gtk.TreeView(listmodel)
> ...
> 
> I'd prefer:
> 
> import gtk
> ...
> win = gtk.Window()
> listmodel = gtk.Liststore(str)
> # now append some column to the model
> 
> # go on
> tv = gtk.Treeview(listmodel)
> 
> Does anybody has an idea, how to solve this problem without using completly 
> the generic model?
> 
> Thanks in advance,
> 
> Volker

Can you fetch your schema before creating the ListStore and then,
knowing the schema at that point, pass the correct types to it?
-- 
Steve McClure   Racemi
email: [EMAIL PROTECTED]  380 Interstate North Pkwy, SE
voice: 404-892-5850 Suite 250
fax: 404-892-7215   Atlanta, GA 30339
http://www.racemi.com

___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/


Re: [pygtk] Liststore model

2007-01-12 Thread Alberto Ruiz

2007/1/12, Volker Helm <[EMAIL PROTECTED]>:


Hi,

I'm trying to implement programm based on a database. Since there are a
lot of columns in a database I'd like to generate a gtk.ListStore model
dynamicly.

normally, I'd do something like this:

import pygtk
...
win = gtk.Window()
listmodel = gtk.ListStore(str,str,str,int,str)
tv = gtk.TreeView(listmodel)
...

I'd prefer:

import gtk
...
win = gtk.Window()
listmodel = gtk.Liststore(str)
# now append some column to the model

# go on
tv = gtk.Treeview(listmodel)

Does anybody has an idea, how to solve this problem without using
completly the generic model?

Thanks in advance,



Sorry, but I don't get what the question is.

Volker




--
Un saludo,
Alberto Ruiz
___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/


[pygtk] Liststore model

2007-01-12 Thread Volker Helm
Hi,

I'm trying to implement programm based on a database. Since there are a lot of 
columns in a database I'd like to generate a gtk.ListStore model dynamicly.

normally, I'd do something like this:

import pygtk
...
win = gtk.Window()
listmodel = gtk.ListStore(str,str,str,int,str)
tv = gtk.TreeView(listmodel)
...

I'd prefer:

import gtk
...
win = gtk.Window()
listmodel = gtk.Liststore(str)
# now append some column to the model

# go on
tv = gtk.Treeview(listmodel)

Does anybody has an idea, how to solve this problem without using completly the 
generic model?

Thanks in advance,

Volker
-- 
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! 
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
___
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/