Skip to content

Instantly share code, notes, and snippets.

@dustractor
Last active November 16, 2023 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dustractor/2130304dbe0f556073d36fe5c744b641 to your computer and use it in GitHub Desktop.
Save dustractor/2130304dbe0f556073d36fe5c744b641 to your computer and use it in GitHub Desktop.
it makes buttons from strings given on the command line and the strings are commands for the buttons to do
#!/usr/bin/env python3
# vim: ft=python
'''
it makes buttons from strings given on the command line
and the strings are commands for the buttons to do
escaping is hard
'''
from argparse import ArgumentParser
from tkinter import Tk,Button
from subprocess import run
from shlex import split
args = ArgumentParser()
args.add_argument("cmds",nargs="*")
ns = args.parse_args()
q = lambda *_:_[0].widget.quit()
pack = dict(fill="both",expand=True)
def button(master,command):
f = lambda:run(split(command))
b = Button(master,text=command,command=f)
b.pack(**pack)
class main(Tk):
def __init__(self):
super().__init__()
self.bind("<Control-q>",q)
self.bind("q",q)
self.bind("<Escape>",q)
self.bind(("Z","Z"),q)
self.bind("<Control-bracketleft>",q)
for cmd in ns.cmds:
button(self,cmd)
main().mainloop()
@dustractor
Copy link
Author

example usage

./tkcommandbuttons.py 'echo foo' 'echo bar'

makes a gui with 2 buttons one echoes foo and the other echoes bar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment