programming
Flip colors or flash text in gtk textbuffers
The get_property() ability is there, but it’s just not as intuitive as it should be. So here’s the method I used to make all the text in a buffer flash by inverting the foreground and background properties. Calling flip_tags() just once will just swap the colors one time, but add this into a gobject.idle_add or a timeout function, and you should get a nice flashing effect.
Avoid looping through treeview iterators with row references
Keeping a dictionary of treerowreferences is the way to go. It makes keeping track of rows a breeze. You don’t have to worry about row locations changing over time as the treerowref keeps track of that automatically.
When you add a parent row to a treestore you get an iterator returned. Use that to get the path of your newly added row and combine the path with the model to get the row reference. You only need to do this once.
|
1 2 3 4 5 |
x = 'track-me' self.rowref = {} titer = treestore.append(None, [x, 'Unknown', None, None, 0] path = treestore.get_path(titer) self.rowref[x] = gtk.TreeRowReference(treestore, path) |
Never iterator through your models again to find a row.
|
1 2 3 4 5 |
it = treestore.get_iter_first() while it: if x == treestore.get_value(it, 0): titer = it it = treestore.iter_next(it) |
That loop can be replaced with this:
|
1 |
titer = treestore.get_iter(self.rowref[x].get_path()) |
However, keeping track of lots of row references takes its toll as well. Everytime you add something, the row references have to adjust themselves. So if you have really large models, try to delete references you won’t need anymore.
Make stock gtk icons from your own images and pixbufs
If you want to set a cell to a pixmap, ok no problem, but what about setting individual liststore values to a pixmap? You can’t do it directly unless you’re using a gtk.STOCK_ image. Here’s a way around that little problem.
Hide a dialog without destroying – pygtk one-liner
If you like the standard window decorations and dont need/want to create your own buttons to handle custom events, closing a dialog without destroying it is a simple one-liner in pygtk. This is helpful when you have logic that replaces individual widgets inside a dialog and you don’t want to recreate the whole dialog.
isset() php function equivalent in python
If you need this, you’re probably doing it wrong. But if you really must check to see if a variable has been assigned yet, there’s nothing really stopping you. Here’s one way of implementing it.
Thread control in python – how to safely stop a thread
So you want to kill a thread… Dangerous. How about asking a thread to die instead? Of course it’s unsafe to just kill a thread dead in its tracks. What if the thread has some resource acquired or is controlling other threads itself? It’s much better to communicate with a thread and tell it to stop, then just wait for it to kill itself.
Use gtk settings to make buttons show their icons again
ou may have noticed gtk has stopped showing images for buttons by default. Many apps have not bothered to check the default settings and just assumed that defining an image for a button meant it would show. Too bad the gtk devs decided to change the default value. But never fear, for there is a way to make sure the image shows without modifying the system or user preferences outside of your application!