Last active
December 29, 2015 11:49
-
-
Save samdroid-apps/7666198 to your computer and use it in GitHub Desktop.
Python3 thumby folder selector. In GTK.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from gi.repository import Gtk | |
path_to_file = {} | |
def getDirs(path, parent_item, tree_data_store): | |
if os.path.exists(path): | |
for file in os.listdir(path): | |
filepath = os.path.join(path, file) | |
if os.path.isdir(filepath): | |
item = tree_data_store.append(parent_item, [file]) | |
path = tree_data_store.get_path(item) | |
path_to_file[str(path)] = filepath | |
getDirs(filepath, item, tree_data_store) | |
def getDirsWithoutRootFolder(path, tree_data_store): | |
if os.path.exists(path): | |
for file in os.listdir(path): | |
filepath = os.path.join(path, file) | |
if os.path.isdir(filepath): | |
getDirs(filepath, None, tree_data_store) | |
def genFileTree(): | |
tree_data_store = Gtk.TreeStore(str) | |
internal_item = tree_data_store.append(None, ["Internal Storage"]) | |
path = tree_data_store.get_path(internal_item) | |
path_to_file[str(path)] = None | |
getDirsWithoutRootFolder("/run/media", tree_data_store) | |
getDirsWithoutRootFolder("/media", tree_data_store) | |
return tree_data_store | |
tree_data_store = genFileTree() | |
win = Gtk.Window() | |
win.connect("delete-event", Gtk.main_quit) | |
def init_dialog(): | |
tree_data_store = genFileTree() | |
tree_view = Gtk.TreeView(tree_data_store) | |
renderer = Gtk.CellRendererText() | |
column = Gtk.TreeViewColumn("Media Folders", renderer, text=0) | |
tree_view.append_column(column) | |
folder_dialog = Gtk.Dialog("Chose a folder") | |
folder_dialog.add_button("Use selected folder", 13) | |
folder_dialog.get_content_area().add(tree_view) | |
folder_dialog.get_content_area().show_all() | |
return folder_dialog, tree_view | |
b = Gtk.Button("Chose External Device") | |
def on_select_file(button): | |
folder_dialog, tree_view = init_dialog() | |
dialog_responce = folder_dialog.run() | |
if dialog_responce == 13: | |
tree_sel = tree_view.get_selection() | |
(tree_store, ti) = tree_sel.get_selected() | |
ids = tree_store.get_path(ti) | |
print(path_to_file[str(ids)]) | |
folder_dialog.destroy() | |
b.connect("clicked", on_select_file) | |
win.add(b) | |
win.show_all() | |
Gtk.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment