Sunday, February 18, 2007

Getting text from a TextView

It took some searching to see how to get the string contained in a TextView with a string (hint: there's no textview.get_text()), but here's the snippet I came up with:

notes_txt = self.widgets.get_widget('notesTxt').get_buffer()
startiter, enditer = notes_txt.get_bounds()
character = dbaccess.fetch_character(self.char_id)
notes_file = open(character['name'] + "-notes.txt", "w")
notes_file.write(notes_txt.get_text(startiter, enditer))
notes_file.close()

Basically, you have to find the start and end Iters to set the bounds of the text you want.

Populating is trivial:

try:
notes_file = open(character['name'] + "-notes.txt", "r")
text = self.widgets.get_widget('notesTxt').get_buffer()
text.set_text(notes_file.read())
notes_file.close()
except:
print "No notes file found: ", character['name'] + \
"-notes.txt"

Saturday, February 10, 2007

Setting the active text on a ComboBox

I had some trouble finding this on the Interweb, so here's how you set the active text on a ComboBox:

def set_combo_value(self, combo, value):
model=combo.get_model()
for item in model:
if item[0]==value:
combo.set_active_iter(item.iter)
break

Setting the active item by index you can get right off the ComboBox API.

Wednesday, February 7, 2007

SQLLite Database Browser

Here's a nice tool for browsing the contents of the Character Manager db:

SQLLite Database Browser

Runs as a single .exe, no dependency downloading nonsense. Excellent work, guys!

Saturday, February 3, 2007

Populating a ComboBox

I'm using Glade to design my UI, which introduced some difficulty in figuring out how to populate my ComboBox (of which there are 2 versions in PyGTK). The PyGTK Tutorial was helpful, and the PyGTK IRC Channel took me the last mile.

The short of it is, a ComboBox works like a TreeView:

# create a store
weapon_store=gtk.ListStore(gobject.TYPE_STRING)
# populate the store
for w in weapon_names:
row=weapon_store.insert_after(None, None)
weapon_store.set_value(row, 0, w)
# get the Glade widget
left_hand_weapon_cbo=self.widgets.get_widget('leftHandWeapon')
left_hand_weapon_cbo.set_model(weapon_store)
# the business bit
cell=gtk.CellRendererText()
left_hand_weapon_cbo.pack_start(cell)
left_hand_weapon_cbo.add_attribute(cell, 'text', 0)
left_hand_weapon_cbo.set_wrap_width(3)


It would be nice if the Glade/PyGTK infrastructure would do more of the work of setting up the widgets with sensible defaults.