tk_utils.py 2 KB
Newer Older
1 2 3 4
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
5 6 7 8
    Provides static method utilities from Tkinter package.
    
    Name: tk_utils.py
    Author: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )
9 10 11 12 13
"""
import os
import tkMessageBox, Tkconstants, tkFileDialog
import tkColorChooser 

14
class Utils(object):
15
    
16 17 18 19
    image_dir = '../data/'
    image_file = 'demo.jpg'
    
    default_directory = '../data/demo'
20 21 22 23 24 25 26 27 28 29
    
    @staticmethod
    def ask_image_name(title = 'Open a image'):
        file_opt = options = {}
        options['defaultextension'] = '.jpg'
        options['filetypes'] = [('All supported files', ("*.GIF", "*.gif", "*.JPG", "*.jpg", "*.JPEG", "*.jpeg", "*.PNG", "*.png", "*.TIF", "*.tif")),
                                ('GIF', ("*.GIF", "*.gif")),
                                ('JPEG', ("*.JPG", "*.jpg", "*.JPEG", "*.jpeg")),
                                ('PNG', ("*.PNG", "*.png")),
                                ('TIF', ("*.TIF", "*.tif"))]
30 31
        options['initialdir'] = Utils.image_dir
        options['initialfile'] = Utils.image_file
32 33 34 35 36
        #options['parent'] = self.__root
        options['title'] = title
        
        filename = tkFileDialog.askopenfilename(**file_opt)
        if filename:
37
            Utils.image_dir, Utils.image_file = os.path.split(filename)
38 39 40
        
        return filename

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

    @staticmethod
    def ask_directory(title = 'Choose a directory', default_dir = None):
        dir_opt = options = {}
        options['initialdir'] = Utils.default_directory if default_dir is None else default_dir
        options['mustexist'] = False
        #options['parent'] = root
        options['title'] = title
        
        directory = tkFileDialog.askdirectory(**dir_opt)
        if directory:
            Utils.default_directory = directory
        
        return directory
        
56 57 58 59
        
    @staticmethod
    def ask_color_choose(title = 'Choose a color', default_color = 'white'):
        return tkColorChooser.askcolor(title=title, initialcolor=default_color)