From 774f2128b4c59561639bd56a1557898633d090af Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 7 Jul 2021 14:19:47 -0400 Subject: [PATCH] initial version --- sysmenu.py | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100755 sysmenu.py diff --git a/sysmenu.py b/sysmenu.py new file mode 100755 index 0000000..7b5fcaf --- /dev/null +++ b/sysmenu.py @@ -0,0 +1,100 @@ +#!/usr/bin/python +from configparser import ConfigParser +from os.path import expanduser +from subprocess import check_output, Popen +from sys import argv + +from tkinter import Button, Label, Frame, SINGLE, Tk + +if len(argv) != 2: + print("sysmenu: invalid number of arguments") + exit(1) + +menu = f'menu:{argv[1]}' + +config = ConfigParser() +config.read(expanduser('~/.config/sysmenu/config')) + +x = config[menu]['x'] or 0 +y = config[menu]['y'] or 0 + +def get_config(section, key, globaldefault = True): + if key in config[section]: + return config[section][key] + elif globaldefault and key in config['global']: + return config['global'][key] + else: + return None + +background = get_config(menu, 'background') or 'white' +foreground = get_config(menu, 'foreground') or 'black' +highlight = get_config(menu, 'highlight') or 'gray' +padding_y = get_config(menu, 'padding_y') or 0 + +root = Tk() +root.title('sysmenu') +root.bind("", exit) +root.bind("", exit) +root.geometry(f'+{x}+{y}') + +if background is not None: + root.configure(background = background); + +components = [] + +def run(command): + Popen(command, shell = True) + exit() + +def beval(expression): + return check_output(f'echo {expression}', shell = True).strip() + +def make_button(name): + spec = config[name] + text = spec['text'] + if('eval' in spec and bool(spec['eval'])): + text = beval(text) + button = Button( + root, + text = text, + command = lambda: run(spec['command']) + ) + button.configure( + borderwidth = 0, + highlightthickness = 0, + bg = background, + fg = foreground, + activeforeground = foreground, + activebackground = highlight, + pady = padding_y + ) + components.append(button) + +def make_label(name): + spec = config[name] + text = spec['text'] + if('eval' in spec): + text = beval(text) + label = Label( + root, + text = text + ) + label.configure( + borderwidth = 0, + highlightthickness = 0, + bg = background, + fg = foreground, + pady = padding_y + ) + components.append(label) + +items = config[menu]['items'].split() +for item in items: + if item.startswith('button:'): + make_button(item) + if item.startswith('label:'): + make_label(item) + +for item in components: + item.pack(fill = 'x') +root.mainloop()