Python GtkTreeview; How to refresh the list when an item is selected
I'm making a rudimentary file manager and am stuck on the question in my post's title. First it looks in a directory, generates a list of items, and appends them to the treeview list.
If a subdirectory is clicked, it becomes the new current directory. All I need is to send this back into the def where it reads and display the current directories contents (I think).
import gtk, os
class PyApp(gtk.Window):
def __init__(self):
super(PyApp, self).__init__()
self.set_size_request(250, 200)
self.set_position(gtk.WIN_POS_CENTER)
self.connect("destroy", gtk.main_quit)
self.set_title("SteveFM")
vbox = gtk.VBox(False, 8)
sw = gtk.ScrolledWindow()
sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
vbox.pack_start(sw, True, True, 0)
store = self.create_model()
treeView = gtk.TreeView(store)
treeView.connect("row-activated", self.on_activated)
treeView.set_rules_hint(True)
sw.add(treeView)
self.create_columns(treeView)
self.add(vbox)
self.show_all()
# Take the names of files and subdirectories in
# the current directory and sort them into a list for treeview
def create_model(self):
store = gtk.ListStore(str)
dirList=os.listdir(os.getcwd())
dircontents = []
for item in dirList:
if item[0] != '.':
if os.path.isdir(item):
dircontents.append(['/'+item])
else:
dircontents.append([item])
dircontents.sort()
for act in dircontents:
store.append([act[0]])
return store
# If a file is clicked, open with leafpad.
# If开发者_运维技巧 a directory is clicked, change the current working directory to it
# and (my hurdle) replace the current treeview list with the contents of the
# new directory, just like a file manager.
def on_activated(self, widget, row, col):
model = widget.get_model()
target = model[row][0]
if target[0] != "/":
cmd = "leafpad "+target
os.system(cmd)
else:
os.chdir(os.getcwd()+target)
print os.getcwd() # Just for debugging
def create_columns(self, treeView):
rendererText = gtk.CellRendererText()
column = gtk.TreeViewColumn(None, rendererText, text=0)
column.set_sort_column_id(0)
treeView.append_column(column)
PyApp()
gtk.main()
Changes I made:
Replace
treeView
withself.treeView
so I can access it easily in other methods.In
on_activated
, create a new model withcreate_model
, and setself.treeView
to use that model usinggtk.TreeView.set_model
.
It seems to work. Code:
import gtk, os
class PyApp(gtk.Window):
def __init__(self):
super(PyApp, self).__init__()
self.set_size_request(250, 200)
self.set_position(gtk.WIN_POS_CENTER)
self.connect("destroy", gtk.main_quit)
self.set_title("SteveFM")
vbox = gtk.VBox(False, 8)
sw = gtk.ScrolledWindow()
sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
vbox.pack_start(sw, True, True, 0)
store = self.create_model()
self.treeView = gtk.TreeView(store)
self.treeView.connect("row-activated", self.on_activated)
self.treeView.set_rules_hint(True)
sw.add(self.treeView)
self.create_columns(self.treeView)
self.add(vbox)
self.show_all()
# Take the names of files and subdirectories in
# the current directory and sort them into a list for treeview
def create_model(self):
store = gtk.ListStore(str)
dirList=os.listdir(os.getcwd())
dircontents = []
for item in dirList:
if item[0] != '.':
if os.path.isdir(item):
dircontents.append(['/'+item])
else:
dircontents.append([item])
dircontents.sort()
for act in dircontents:
store.append([act[0]])
return store
# If a file is clicked, open with leafpad.
# If a directory is clicked, change the current working directory to it
# and (my hurdle) replace the current treeview list with the contents of the
# new directory, just like a file manager.
def on_activated(self, widget, row, col):
model = widget.get_model()
target = model[row][0]
if target[0] != "/":
cmd = "leafpad "+target
os.system(cmd)
else:
os.chdir(os.getcwd()+target)
print os.getcwd() # Just for debugging
self.treeView.set_model(self.create_model())
def create_columns(self, treeView):
rendererText = gtk.CellRendererText()
column = gtk.TreeViewColumn(None, rendererText, text=0)
column.set_sort_column_id(0)
self.treeView.append_column(column)
PyApp()
gtk.main()
精彩评论