Satya's blog - 2007/10/
This could be a long-ish rant. Some of it is ROT13-encrypted and by the power of DMCA you are forbidden to decode it. When I was a kid, in school and stuff, I was always chastised for spending so much time on the computer. But now I work with computers and I'm good at it (modesty? screw you. only a few thousand people can do what I do). I wouldn't be as proficient as I am now without having spent so much time with computers. Sure, it's important to learn the other things too, but if I hadn't stuck with it then, I wouldn't be here now. Even the games are an important par of it -- consider the saved game files, and backing up of those files. Taught me about the importance of backups. Then in the mdoern day I still get the whole spending too much time on the computer crap. You know what, you can edit your own damn videos and host them on your own damn web server and put up your own pictures and don't come crying to me when Yahoo or whoeevr eats them. And no, I qba'g xabj ubj gb znxr onpxhc be rnfl-cynl pbcvrf bs gubfr zhfvp PQf, orpnhfr you won't give me time to figrue it out. And no, I can't do it because I don't have your fancy Windows tools, I only have Linux, which obviously can't do that fancy multi-media shit. (Not true, it can, but that's *your* stance, so screw you guys, I'm going home.) Oh, you don't have the tools? Sucks to be you. Yeah, go ahead and download that free shareware, it's not going to screw up your computer. Oh, it installed spyware? Well, if you hadn't dismissed my warnings the last 1024 times I told you about spyware, maybe that wouldn't have happened. Tnu, crbcyr cvff zr bss. Qba'g yvxr pbzchgref? QBa'g hfr gurz. Pnyy zr trrx naq areq va n qrebtngbel jnl? Jryy, jba'g uryc lbh erfgber lbhe cbea sebz gur yngrfg fcljner shpxhc. Ungr grpuabybtl? Lbh pna'g unir nal, gura. Ab, abg rira pnef, yvtugvat, gryrcubarf, sbbq.... Jnyx nebhaq arxxvq. Onfvpnyyl, shpx bss.
AT&T got broken up in the 1970s or so, right? And in "A New Hope", the Death Star was destroyed (coincidentally, in 1977 -- or was it 78?). Now we have AT&T re-merging. And in "Return of the Jedi" we had a newly built Death Star, which the Emperor said was "fully armed and operational". Also, consider that the logo looks like the DS.
Let's say you want to display a nice list of cities, grouped by state, like so:
In a GUI window of a Python PyGTk app, of course. Let's also say that the list will have that little collapsing triangle next to each state, so you can close and open the sub-list of cities. As a bonus, let's make the columns sortable. What you need is GtkTreeView. What you want is something simpler. Sucks to be you. Assuming you know how to do the rest, let's concentrate on the TreeView itself. It's sort-of MVC-ish, so your TreeView needs a TreeModel or rather, a TreeStore: treestore=gtk.TreeStore(str,str) for state in states: piter=treestore.append(None, [state['name'], None]) for city in states['cities']: treestore.append(piter, [None, city])
So, we instantiate a treestore object and tell it that we want two string
columns.
Next we loop through the list of states. Each item is a dictionary, the name
and the cities list. The line that starts Now we set up a sortable tree model using this treestore, and a treeview using the sortable tree model: tmsort = gtk.TreeModelSort(treestore) # produce a sortable treemodel treeview = gtk.TreeView(tmsort) # the tree view Now, for each column (State and City), we do the following:
The last two lines make the column searchable and sortable. tvcolumns={} # the columns cells={} # the cells i=0 for c in ('State','City'): # the actual column headers # instantiate TVC tvcolumns[c] = gtk.TreeViewColumn(c) # add to the treeview treeview.append_column(tvcolumns[c]) # instantiate and add the cell object cells[c]=gtk.CellRendererText() tvcolumns[c].pack_start(cells[c], True) #add the cell to the column, allow it to expand # now set the cell's text attribute to the treeview's column i (0,1) tvcolumns[c].add_attribute(cells[c], 'text', i) #make it searchable and sortable treeview.set_search_column(i) tvcolumns[c].set_sort_column_id(i) i+=1 Whee! Now we just need to put this in a scrolled window in case it's too big for our containing widget: treeview.show() scrolled_window = gtk.ScrolledWindow() scrolled_window.add_with_viewport(treeview) scrolled_window.set_size_request(300,200) scrolled_window.show()
Suppose we want to do something, like open a window showing the city weather,
when a city is double-clicked. Let us connect a signal handler to the treeview
(I believe it might be easier to connect one to each row or cell, but less
efficient). The signal we want to look for is treeview.connect("row-activated", self.city_clicked, None) And that's it. That should show our nice grouped table when run (and added to a main window, etc).
But the most confusing and poorly documented bit for
me was the signal handler, def city_clicked(self, treeview, iter, tvc, foo): model=treeview.get_model() iter = model.get_iter(iter) city_name = model.get_value(iter, 1) # column id 1 contains the city name The first parameter to city_clicked is self, because we're in a class, remember? The next two are the treeview object and the iter object that was activated. Next is tvc, the TreeViewColumn object for the cell that was clicked. The last one, foo, is the extra data, in this case None.
So we get the TreeView's underlying model. The I should write a wrapper class for this stuff. It's way too much work as it stands. Update: Or, you can use glade. It's a user interface layout designer thingy. See http://www.learningpython.com/...g-pygtk-and-glade/ On the other hand, all that does is build the interface, it will NOT populate your treeview. That's not a UI function -- not in the view, in MVC terms -- it's in the Model or Controller, i.e. in your python code, as above. Last updated: Oct 13 2007 10:36
I've been doing a lot of Ruby and Ruby on Rails programming recently. I did a lot of Python last weekend. Yesterday I had to do some Perl for some quick-and-dirty one-off (I hope) stuff. It felt clumsy. Ruby does not have braces, unlike C and Perl. Code blocks are delimited by a keyword and 'end'. Python loses the 'end' and relies on indentation (also icky). Perl? Semi-colons everywhere, braces everywhere. I forgot the end of line semi-colons many times. And I'm a gosh-darn perl expert! |
|