use native select folder dialog
This commit is contained in:
parent
1d52413618
commit
b2368b9053
6 changed files with 94 additions and 46 deletions
|
|
@ -35,6 +35,8 @@ def selectFolder(data):
|
|||
cmd = ['./ctl', 'ui', 'folder']
|
||||
if sys.platform == 'win32':
|
||||
cmd = win32_ui('folder')
|
||||
if 'base' in data:
|
||||
cmd += [data['base']]
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True)
|
||||
stdout, stderr = p.communicate()
|
||||
path = stdout.decode('utf-8').strip()
|
||||
|
|
@ -53,6 +55,8 @@ def selectFile(data):
|
|||
cmd = ['./ctl', 'ui', 'file']
|
||||
if sys.platform == 'win32':
|
||||
cmd = win32_ui('file')
|
||||
if 'base' in data:
|
||||
cmd += [data['base']]
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
|
||||
stdout, stderr = p.communicate()
|
||||
path = stdout.decode('utf-8').strip()
|
||||
|
|
|
|||
46
oml/ui.py
46
oml/ui.py
|
|
@ -3,8 +3,9 @@
|
|||
import sys
|
||||
import os
|
||||
try:
|
||||
import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
from gi.repository import Gtk, GObject
|
||||
GObject.threads_init()
|
||||
use_Gtk = True
|
||||
except:
|
||||
from tkinter import Tk, PhotoImage
|
||||
|
|
@ -13,13 +14,19 @@ except:
|
|||
|
||||
DEBUG = False
|
||||
|
||||
|
||||
def short_home(path):
|
||||
home = os.path.expanduser('~')
|
||||
if path and path.startswith(home):
|
||||
path = path.replace(home, '~')
|
||||
return path
|
||||
|
||||
class GtkUI:
|
||||
def selectFolder(self, data):
|
||||
dialog = Gtk.FileChooserDialog(data.get("title", "Select Folder"),
|
||||
None,
|
||||
Gtk.FileChooserAction.SELECT_FOLDER,
|
||||
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
|
||||
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
|
||||
dialog = Gtk.FileChooserDialog(title=data.get("title", "Select Folder"),
|
||||
action=Gtk.FileChooserAction.SELECT_FOLDER)
|
||||
dialog.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
|
||||
Gtk.STOCK_OPEN, Gtk.ResponseType.OK)
|
||||
dialog.set_default_response(Gtk.ResponseType.OK)
|
||||
|
||||
response = dialog.run()
|
||||
|
|
@ -36,14 +43,13 @@ class GtkUI:
|
|||
Gtk.main_iteration()
|
||||
if DEBUG:
|
||||
print("done")
|
||||
return filename
|
||||
return short_home(filename)
|
||||
|
||||
def selectFile(self, data):
|
||||
dialog = Gtk.FileChooserDialog(data.get("title", "Select File"),
|
||||
None,
|
||||
Gtk.FileChooserAction.OPEN,
|
||||
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
|
||||
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
|
||||
dialog = Gtk.FileChooserDialog(title=data.get("title", "Select File"),
|
||||
action=Gtk.FileChooserAction.OPEN)
|
||||
dialog.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
|
||||
Gtk.STOCK_OPEN, Gtk.ResponseType.OK)
|
||||
dialog.set_default_response(Gtk.ResponseType.OK)
|
||||
|
||||
response = dialog.run()
|
||||
|
|
@ -60,7 +66,7 @@ class GtkUI:
|
|||
Gtk.main_iteration()
|
||||
if DEBUG:
|
||||
print("done")
|
||||
return filename
|
||||
return short_home(filename)
|
||||
|
||||
|
||||
class TkUI:
|
||||
|
|
@ -77,10 +83,12 @@ class TkUI:
|
|||
self.root.after_idle(self.root.call, 'wm', 'attributes', '.', '-topmost', False)
|
||||
|
||||
def selectFolder(self, data):
|
||||
return tkinter.filedialog.askdirectory(parent=self.root, title=data.get("title", "Select Folder"))
|
||||
folder = tkinter.filedialog.askdirectory(parent=self.root, title=data.get("title", "Select Folder"))
|
||||
return short_home(folder)
|
||||
|
||||
def selectFile(self, data):
|
||||
return tkinter.filedialog.askopenfilename(parent=self.root, title=data.get("title", "Select File"))
|
||||
filename = tkinter.filedialog.askopenfilename(parent=self.root, title=data.get("title", "Select File"))
|
||||
return short_home(filename)
|
||||
|
||||
|
||||
if use_Gtk:
|
||||
|
|
@ -89,8 +97,12 @@ else:
|
|||
ui = TkUI()
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
if len(sys.argv) == 2 and sys.argv[1] == 'folder':
|
||||
base = '~'
|
||||
if len(sys.argv) >= 3:
|
||||
base = sys.argv[2]
|
||||
base = os.path.expanduser(base)
|
||||
os.chdir(base)
|
||||
if len(sys.argv) >= 2 and sys.argv[1] == 'folder':
|
||||
print(ui.selectFolder({}))
|
||||
else:
|
||||
print(ui.selectFile({}))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue