Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

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.

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.

Monday, January 22, 2007

First Thoughts

Glade is quite nice apart from the GIMP-style multi window UI. It's incredibly frustrating to have a web page with instructions (or another app with a mockup) underneath Glade, then click that app and then have to raise each of the four Glade windows separately. Makes absolutely no sense. A friend of mine suggests that's what multiple desktops are for, but this is a clumsy workaround.

Found a good tutorial on Glade + Python. It's all text, and somewhat outdated, but was the most helpful of the stuff I read.

First Post

This is experimental blog tied to the development Character Manager application. This will be the journal for development (containing editorial content), leaving the wiki for "factual" data. The Character Manager application is a simple UI for managing characters used in the Dungeons and Dragons game. It is inspired by RedBlade, but it's chief purpose is to teach me Python + GTK UI development.