openmedialibrary/oml/ui.py

83 lines
2.6 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
DEBUG=False
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
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.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"),
None,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
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()
self.root.withdraw() #hiding tkinter window
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({}))