openmedialibrary/oml/ui.py

91 lines
2.9 KiB
Python
Raw Normal View History

2014-05-16 15:48:48 +00:00
# encoding: utf-8
# vi:si:et:sw=4:sts=4:ts=4
2019-01-21 05:09:08 +00:00
import sys
2014-05-16 15:48:48 +00:00
try:
from gi.repository import Gtk, GObject
GObject.threads_init()
use_Gtk = True
except:
2014-09-02 22:32:44 +00:00
from tkinter import Tk
import tkinter.filedialog
2014-05-16 15:48:48 +00:00
use_Gtk = False
2019-01-21 05:09:08 +00:00
DEBUG = False
2014-05-16 15:48:48 +00:00
class GtkUI:
def selectFolder(self, data):
dialog = Gtk.FileChooserDialog(data.get("title", "Select Folder"),
2016-06-24 10:27:05 +00:00
None,
Gtk.FileChooserAction.SELECT_FOLDER,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
2014-05-16 15:48:48 +00:00
dialog.set_default_response(Gtk.ResponseType.OK)
response = dialog.run()
if response == Gtk.ResponseType.OK:
filename = dialog.get_filename()
if DEBUG:
2014-09-02 22:32:44 +00:00
print(filename, 'selected')
2014-05-16 15:48:48 +00:00
elif response == Gtk.ResponseType.CANCEL:
if DEBUG:
2014-09-02 22:32:44 +00:00
print('Closed, no files selected')
2014-05-16 15:48:48 +00:00
filename = None
dialog.destroy()
while Gtk.events_pending():
Gtk.main_iteration()
if DEBUG:
2014-09-02 22:32:44 +00:00
print("done")
2014-05-16 15:48:48 +00:00
return filename
def selectFile(self, data):
dialog = Gtk.FileChooserDialog(data.get("title", "Select File"),
2016-06-24 10:27:05 +00:00
None,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
2014-05-16 15:48:48 +00:00
dialog.set_default_response(Gtk.ResponseType.OK)
response = dialog.run()
if response == Gtk.ResponseType.OK:
filename = dialog.get_filename()
if DEBUG:
2014-09-02 22:32:44 +00:00
print(filename, 'selected')
2014-05-16 15:48:48 +00:00
elif response == Gtk.ResponseType.CANCEL:
if DEBUG:
2014-09-02 22:32:44 +00:00
print('Closed, no files selected')
2014-05-16 15:48:48 +00:00
filename = None
dialog.destroy()
while Gtk.events_pending():
Gtk.main_iteration()
if DEBUG:
2014-09-02 22:32:44 +00:00
print("done")
2014-05-16 15:48:48 +00:00
return filename
class TkUI:
def __init__(self):
self.root = Tk()
2019-01-21 05:09:08 +00:00
if sys.platform == 'darwin':
self.root.lift()
self.root.call('wm', 'attributes', '.', '-topmost', True)
self.root.update()
self.root.after_idle(self.root.call, 'wm', 'attributes', '.', '-topmost', False)
2016-06-24 10:27:05 +00:00
self.root.withdraw() # hiding tkinter window
2014-05-16 15:48:48 +00:00
def selectFolder(self, data):
2014-09-02 22:32:44 +00:00
return tkinter.filedialog.askdirectory(title=data.get("title", "Select Folder"))
2014-05-16 15:48:48 +00:00
def selectFile(self, data):
2014-09-02 22:32:44 +00:00
return tkinter.filedialog.askopenfilename(title=data.get("title", "Select File"))
2014-05-16 15:48:48 +00:00
if use_Gtk:
ui = GtkUI()
else:
ui = TkUI()
if __name__ == '__main__':
import sys
if len(sys.argv) == 2 and sys.argv[1] == 'folder':
2014-09-02 22:32:44 +00:00
print(ui.selectFolder({}))
2014-05-16 15:48:48 +00:00
else:
2014-09-02 22:32:44 +00:00
print(ui.selectFile({}))