diff options
-rwxr-xr-x | logout-manager-tcl.py | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/logout-manager-tcl.py b/logout-manager-tcl.py index c8ba228..4d823d5 100755 --- a/logout-manager-tcl.py +++ b/logout-manager-tcl.py @@ -20,17 +20,14 @@ # homedir https://stackoverflow.com/questions/4028904/how-to-get-the-home-directory-in-python # natural sort https://stackoverflow.com/questions/46228101/sort-list-of-strings-by-two-substrings-using-lambda-function/46228199#46228199 # tooltips https://stackoverflow.com/questions/3221956/how-do-i-display-tooltips-in-tkinter/41381685#41381685 -# rsvg https://stackoverflow.com/questions/10393675/rsvg-with-python-3-2-on-ubuntu/19744099#19744099 -# svg for tkinter http://code.activestate.com/lists/python-list/595078/ # Improve: -# add svg support # Dependencies: # package: RPM | DPKG # tkinter: python36-tkinter | python3-tk # PIL: python36-pillow-tk | python3-pil.imagetk # pip3 install cairosvg -import glob, re, cairosvg +import glob, re import tkinter as tk from functools import partial from pathlib import Path @@ -40,6 +37,14 @@ from PIL import Image, ImageTk path.append("/home/bgirton/dev/logout-manager") import lmlib +LM_USE_SVG = 0 +try: + from cairosvgf import svg2png + LM_USE_SVG = 1 +except: + print("WARNING: Unable to import cairosvg. No svg images will be displayed.") + LM_USE_SVG = 0 + config = lmlib.Initialize_config() actions = lmlib.Actions @@ -246,14 +251,15 @@ def get_filename_of_icon(name, theme = "hicolor", size = 48, category = "actions # first, find all files underneath /usr/share/icons/$THEME/$SIZE print("Finding filename of icon, theme=",theme,"category=",category,"name=",name) - # IMPROVEHERE: this glob affects if scalable/ dir is included. When support for svg is added (which is beyond PIL), need to add a conditional and use a second glob if USE_SVG=1. # to exclude the scalable/ contents, replace dir 5 asterisk with [0-9]* results = [] - # COMMENTED to test with the svg. results = glob.glob("/usr/share/icons/"+theme+"/*/"+category+"/"+name+".{png,PNG}") - results = glob.glob("/usr/share/icons/"+theme+"/*/"+category+"/"+name+".*") + file_filters = ".*" + if LM_USE_SVG == 0: + file_filters = ".{png,PNG}" + results = glob.glob("/usr/share/icons/"+theme+"/*/"+category+"/"+name+file_filters) if len(results) == 0: # no results with that category, so try all categories - results = glob.glob("/usr/share/icons/"+theme+"/*/*/"+name+".*") + results = glob.glob("/usr/share/icons/"+theme+"/*/*/"+name+file_filters) # the sort arranges it so a Numix/24 dir comes before a Numix/24@2x dir results = sorted(results, key=sort_sizes) #print(results) @@ -263,14 +269,18 @@ def get_filename_of_icon(name, theme = "hicolor", size = 48, category = "actions def photoimage_from_svg(filename = "",size = "48"): # this one works, but does not allow me to set the size. + # this is kept as an example of how to open a svg without saving to a file. # open svg - item = cairosvg.svg2png(url=filename, parent_width = size, parent_height = size) + item = svg2png(url=filename, parent_width = size, parent_height = size) return ImageTk.PhotoImage(data=item) def image_from_svg(filename = "",size = "48"): # open svg - cairosvg.svg2png(url=filename,write_to="/tmp/file3.png",parent_width = size,parent_height = size) - photo = Image.open("/tmp/file3.png") + if LM_USE_SVG == 1: + svg2png(url=filename,write_to="/tmp/lm_temp_image.png",parent_width = size,parent_height = size) + photo = Image.open("/tmp/lm_temp_image.png") + else: + photo = Image.new("RGBA",[size,size]) return photo def get_scaled_icon(icon_name, size = 24, icon_theme = "default", fallback_icon_name = ""): @@ -298,8 +308,9 @@ def get_scaled_icon(icon_name, size = 24, icon_theme = "default", fallback_icon_ photo = image_from_svg(filename=iconfilename, size=size) else: photo = Image.open(iconfilename) - except: + except Exception as f: print("Error with icon file.") + print(f) return None photo.thumbnail(size=[size, size]) try: |