Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gi
- import sys
- import os
- gi.require_version('Gtk', '3.0')
- from gi.repository import Gtk, Gdk, GObject
- class SimpleTreeview():
- def main(self):
- self.lsFiles = Gtk.ListStore(GObject.TYPE_STRING)
- self.refresh_data(self.lsFiles)
- self.display_window()
- def refresh_data(self, liststore):
- liststore.clear()
- for r, d, f in os.walk("/home/user/tmp"):
- for file in f:
- liststore.append([os.path.join(r, file)])
- def on_click_fileSelect(self, tree_view, path, column):
- selection = tree_view.get_selection()
- value = selection.get_selected_rows()
- if value:
- model, iter = value
- ret = model.get_value(model.get_iter(iter[0]), 0)
- if sys.platform.startswith('linux'):
- subprocess.Popen(['xdg-open', ret])
- def on_drag_data_get(self, treeview, context, selection, info, time):
- data = []
- treeselection = treeview.get_selection()
- model, rows = treeselection.get_selected_rows()
- for row in rows:
- treeselection.select_path(row)
- value = model[row][0]
- data.append(value)
- text = str(os.linesep).join(data)
- selection.set_text(text, -1)
- return True
- def display_window(self):
- self.win = Gtk.Window()
- self.win.connect("destroy", Gtk.main_quit)
- self.layout = Gtk.VBox()
- self.win.add(self.layout)
- self.resultBox = Gtk.Box()
- self.fileSelect = Gtk.TreeView(model=self.lsFiles)
- self.fileSelect.get_selection().set_mode(Gtk.SelectionMode.SINGLE)
- cell = Gtk.CellRendererText()
- col = Gtk.TreeViewColumn("Filename", cell, text=0)
- col.set_sort_column_id(0)
- self.fileSelect.append_column(col)
- scroll = Gtk.ScrolledWindow()
- scroll.add(self.fileSelect)
- self.fileSelect.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK,
- [('text/plain', Gtk.TargetFlags.OTHER_APP, 0)], Gdk.DragAction.DEFAULT | Gdk.DragAction.COPY)
- self.fileSelect.drag_source_add_text_targets()
- self.fileSelect.connect('row_activated', self.on_click_fileSelect)
- self.fileSelect.connect("drag-data-get", self.on_drag_data_get)
- self.fileSelect.columns_autosize()
- self.resultBox.pack_start(scroll, True, True, 0)
- self.layout.pack_start(self.resultBox, True, True, 0)
- self.win.set_default_size(800, 600)
- self.win.show_all()
- Gtk.main()
- if __name__ == "__main__":
- SimpleTreeview().main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement