[pygtk] TreeView selection issue
Alan F
matopoftheworld at live.co.uk
Fri Jan 30 08:19:53 WST 2009
> Is there some reason why you are not using the TreeSelection "changed"
> signal to track the selction changes and update your display? In your
> case I assume that only one selection at a time is allowed so the
> "changed" signal would seem to be ideal.
>
> John
Thanks John. Finally got it working.
If anybodys interested, I've just created a program that displays the tags of mp3 files in a selected folder. It's quite basic as you have to enter path into script file.
# ######### Start of File
#!/usr/bin/env python
# tag-tree.py
import pygtk
pygtk.require('2.0')
import gtk
import os
import glob
from mutagen.id3 import ID3, TPE2, TIT2, TPE1, TALB, TCOM, TDRC, TCON, TRCK, TPOS
MUSIC_DIR = "ENTER MUSIC FOLDER PATH HERE"
class tag_tree:
# close the window and quit
def delete_event(self, widget, event, data=None):
gtk.main_quit()
return False
def add_data(self):
os.chdir(MUSIC_DIR)
file_list = glob.glob('*.mp3')
first_time = True
if len(file_list) == 0:
print "No mp3 files in folder"
else:
file_list.sort()
for x in file_list:
curr_iter = self.liststore.append([x,x] )
if first_time == True:
first_iter = curr_iter
first_time = False
return first_iter
def change_details(self, widget, txt_area):
#get data from highlighted selection
treeselection = self.treeview.get_selection()
(model, iter) = treeselection.get_selected()
if iter != None :
# Add audio data
txt_buffer = gtk.TextBuffer()
file_name = self.liststore.get_value(iter, 0)
audio = ID3(file_name)
if audio.has_key('TIT2'):
txt_buffer.insert_at_cursor("Title: " + unicode(audio["TIT2"]) + '\n')
if audio.has_key('TPE1'):
txt_buffer.insert_at_cursor( "Artist: " + unicode(audio["TPE1"]) + '\n')
if audio.has_key('TCOM'):
txt_buffer.insert_at_cursor("Composer: " + unicode(audio["TCOM"]) + '\n')
if audio.has_key('TPE2'):
txt_buffer.insert_at_cursor("AlbumArtist: " + unicode(audio["TPE2"]) + '\n')
if audio.has_key('TALB'):
txt_buffer.insert_at_cursor("Album: " + unicode(audio["TALB"]) + '\n')
if audio.has_key('TCON'):
txt_buffer.insert_at_cursor("Genre: " + unicode(audio["TCON"]) + '\n')
if audio.has_key('TDRC'):
txt_buffer.insert_at_cursor("Year: " + unicode(audio["TDRC"]) + '\n')
if audio.has_key('TRCK'):
txt_buffer.insert_at_cursor("Track num: " + unicode(audio["TRCK"]) + '\n')
if audio.has_key('TPOS'):
txt_buffer.insert_at_cursor("Disc num: " + unicode(audio["TPOS"]) + '\n')
txt_area.set_buffer(txt_buffer)
def __init__(self):
# Create a new window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("Tag Display")
self.window.set_size_request(500, 200)
self.window.connect("delete_event", self.delete_event)
table1 = gtk.Table(1, 2, False)
table1.set_row_spacings( 5)
table1.set_col_spacings(5)
self.window.add(table1)
# create a ListStore with one string column to use as the model
self.liststore = gtk.ListStore(str, str)
first_iter = self.add_data()
# create the TreeView using liststore
self.treeview = gtk.TreeView(self.liststore)
self.treeview.set_headers_visible(False)
# create the TreeViewColumn to display the data
self.tvcolumn = gtk.TreeViewColumn()
# add tvcolumn to treeview
self.treeview.append_column(self.tvcolumn)
# create a CellRendererText to render the data
self.cell = gtk.CellRendererText()
# add the cell to the tvcolumn and allow it to expand
self.tvcolumn.pack_start(self.cell, True)
# set the cell "text" attribute to column 0 - retrieve text
# from that column in treestore
self.tvcolumn.add_attribute(self.cell, 'text', 0)
# make it searchable
self.treeview.set_search_column(0)
# Allow drag and drop reordering of rows
self.treeview.set_reorderable(False)
# values colx, coly, roxx, rowy
table1.attach(self.treeview, 0, 1, 0, 1)
txt_area = gtk.TextView()
txt_area.set_editable(False)
table1.attach(txt_area, 1,2, 0, 1)
self.treeview.connect('cursor-changed', self.change_details, txt_area)
first_path = self.liststore.get_path(first_iter)
self.treeview.set_cursor(0)
self.window.show_all()
table1.show()
def main():
gtk.main()
if __name__ == "__main__":
tag_tree()
main()
# ######### End of File
_________________________________________________________________
Twice the fun—Share photos while you chat with Windows Live Messenger. Learn more.
http://www.microsoft.com/uk/windows/windowslive/products/messenger.aspx
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.daa.com.au/pipermail/pygtk/attachments/20090129/9a40e722/attachment.htm
More information about the pygtk
mailing list