Newbie in the deep - some PyGTK questions

N

nmp

Hi, I have recently (a few days ago) started programming again. As a hobby
for now, but once I become a bit more proficient at Python I may have an
idea or two for some small scale "commercial" projects.

I have a fair understanding of OO concepts having done some Smalltalk in
the (now distant) past. Thought I should mention that for some weird
reason, heh never mind. I may still be a quick learner but I am probably
*way* behind on modern application programming... ;)

My development environment at the moment is Python, PyGTK, Glade and SPE
on Ubuntu 6.10. I am using Dive into Python, www.learnpython.com and such
sources as references, but mostly I try to learn by doing.

First "problem": when I insert the glade file in my application the way
some tutorials have shown me:

self.wTree = gtk.glade.XML("autostart.glade", "mainWindow")

.... Python is consistently giving me this warning:

/home/xxxx/Projecten/autostart/autostart.py:18: GtkWarning:
gtk_widget_grab_default: assertion `GTK_WIDGET_CAN_DEFAULT (widget)'
failed
self.wTree = gtk.glade.XML("autostart.glade", "mainWindow")

It doesn't seem to be a big deal because the program does run and the
window is shown correctly et cetera. However it bugs me, because I don't
*understand* what it is that I am doing wrong. A Google search on this
warning turns up some posts of people asking the same question, but no
answers as far as I can tell... So, how do I get rid of this warning?

Second "problem": I have discovered the Gtk.TreeView and managed to load a
CSV file into it as a primitive database of sorts. And all this with just a
few lines of code, really awesome. However, this TreeView business
confuses me. One of the fields in my database is a boolean (so my
ListModel, I guess it is called, is (str, str, bool)). Imagine something
like this:

Author | Title | In collection?
---------------------+---------------------------+---------------
Darwin, Charles | On the Origin of Species | FALSE
Shakespeare, William | A Midsummer Night's Dream | TRUE
etc. | |

Now I want the user of my program to be able to just double click a row or
hit Enter to flip the boolean, so FALSE becomes TRUE and vice versa.

I got as far as figuring out that I will need to connect a handler for the
row_activated signal. But there it stops for me, I can't quite get my head
around what should happen next. The handler will be passed a Gtk.TreePath
object from which I should be able to extract which row was affected. Now
I *think* that it should be just two or three lines of code, but I can't
figure it out. Haven't seen anything helpful elsewhere, either. I hope
that someone here can give me the key to this, so that I can open the door
myself, and perhaps then I will finally "get" it.

The third "problem" (for now...) is really just a cosmetic thing. The
representation of the boolean in the list is fine for me, but in time I
would like to make it prettier by replacing it with a graphical checkbox.
Does anyone have an example of this that I can use?

Thank you for your patience. I can already see how all this is going to
keep me busy for weeks and it will be fun ;)
 
D

Dieter Verfaillie

Hi,

First "problem": when I insert the glade file in my application the way
some tutorials have shown me:

self.wTree = gtk.glade.XML("autostart.glade", "mainWindow")

... Python is consistently giving me this warning:

/home/xxxx/Projecten/autostart/autostart.py:18: GtkWarning:
gtk_widget_grab_default: assertion `GTK_WIDGET_CAN_DEFAULT (widget)'
failed
self.wTree = gtk.glade.XML("autostart.glade", "mainWindow")

I think you've set the "Has default" property to Yes on some
widget, but you forgot to set the "Can default" property
to Yes on that same widget.
In short, you created a default widget that can't be the default and
gtk happily complains about that. Check your glade file to fix it.
Now I want the user of my program to be able to just double click a row or
hit Enter to flip the boolean, so FALSE becomes TRUE and vice versa.

See the answer to your third "problem" :)
The third "problem" (for now...) is really just a cosmetic thing. The
representation of the boolean in the list is fine for me, but in time I
would like to make it prettier by replacing it with a graphical checkbox.
Does anyone have an example of this that I can use?

Use a CellRendererToggle. There's a tutorial on the pygtk website:
http://pygtk.org/pygtk2tutorial/sec-CellRenderers.html (scroll all the
way down that page to "Figure 14.6. Editable and Activatable Cells")
Thank you for your patience. I can already see how all this is going to
keep me busy for weeks and it will be fun ;)

You'll probably get faster/better answers to you pygtk questions on the
pygtk mailing list: http://www.daa.com.au/mailman/listinfo/pygtk

Have fun,
Dieter
 
N

nmp

Op Sat, 24 Feb 2007 15:26:14 +0100, schreef Dieter Verfaillie:
Hi,



I think you've set the "Has default" property to Yes on some
widget, but you forgot to set the "Can default" property
to Yes on that same widget.
In short, you created a default widget that can't be the default and
gtk happily complains about that. Check your glade file to fix it.

I did that and it works now :) Thank you, I had indeed been playing with
those options but not being entirely sure what they did I also forgot
about them...
See the answer to your third "problem" :)


Use a CellRendererToggle. There's a tutorial on the pygtk website:
http://pygtk.org/pygtk2tutorial/sec-CellRenderers.html (scroll all the
way down that page to "Figure 14.6. Editable and Activatable Cells")

Yes, I found that a short time after I posted my question and this seems
to be the way to go indeed. I already copied some code from somewhere that
worked. Now, it could just be me but I *still* find the Gtk/TreeView thing
confusing. To master it will take me a bit more time than I thought, but
it seems to be one of the most important pieces of the toolkit so I see I
will pretty much have to invest that time. Thank you for mentioning it too.
You'll probably get faster/better answers to you pygtk questions on the
pygtk mailing list: http://www.daa.com.au/mailman/listinfo/pygtk

Will look into that the next few days, if nothing else comes up such as
"real life" distractions :)
 
D

Dieter Verfaillie

I did that and it works now :) Thank you, I had indeed been playing with
those options but not being entirely sure what they did I also forgot
about them...

Happy it worked out :)
Yes, I found that a short time after I posted my question and this seems
to be the way to go indeed. I already copied some code from somewhere that
worked. Now, it could just be me but I *still* find the Gtk/TreeView thing
confusing. To master it will take me a bit more time than I thought, but
it seems to be one of the most important pieces of the toolkit so I see I
will pretty much have to invest that time. Thank you for mentioning it too.

Yep, it can be confusing at first, but once you get your head wrapped
around it, it's a really powerful system. I've dug up some bookmarks
about the GtkTree(View|Model|Store) and its Model/View/Controller
approach. They might help you understand the big picture
- a short overview:
http://liw.iki.fi/liw/texts/gtktreeview-tutorial.html
- and as noted on that page, Tim Müller's tutorial:
http://scentric.net/tutorial/
This one uses C, but is still very informative, even for a python
programmer.

There's also a lot of information in section 13 of the pygtk faq:
http://www.async.com.br/faq/pygtk/index.py?req=index

hth,
Dieter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top