Skip to content

Instantly share code, notes, and snippets.

@mdmitry1
Last active February 21, 2024 20:42
Show Gist options
  • Save mdmitry1/e06eda238d740f41989a5c3af3073e8a to your computer and use it in GitHub Desktop.
Save mdmitry1/e06eda238d740f41989a5c3af3073e8a to your computer and use it in GitHub Desktop.
Python Tkinter window running Tcl script
#!/usr/bin/tcsh -f
set w=`xdotool getwindowfocus`
set g=`xdotool getwindowgeometry $w | grep : \
| sort | awk '{print $2}' | tr '\012' ' ' | sed -e 's/ /+/' -e 's/,/+/' -e 's/$//'`
echo $g
#!/usr/bin/tclsh
package require Tclx
while {1} {
sleep 1
puts "Waiting for keypress..."
}
#!/usr/bin/python3.11
from functools import partial
from os import kill,_exit,system
from os.path import realpath, split
from pynput.keyboard import Listener
from signal import SIGINT
from subprocess import Popen
from sys import argv
from tkinter import Button, Text, Tk
import sh
class Process:
def __init__(self): self._pid = 0
def get(self): return self._pid
def run_process(script,p): p = Popen(script).pid
def kill_process(pinst,key):
print('You pressed ', key)
kill(pinst.get(), SIGINT)
_exit(0)
parent_window_geometry=sh.get_window_geometry()
g_split0=parent_window_geometry.split('\n')[0].split('x')
g_split1=g_split0[1].split("+")
w=int(g_split0[0])
h=int(g_split1[0])
x=int(g_split1[1])
y=int(g_split1[2])
root_w=400
root_h=40
root_x=int(x+w/2-root_w/2)
root_y=int(y+h/2-root_h/2)
p = Process()
listener = Listener(on_press=partial(kill_process, p)).start()
print("Started keyboard listener")
script_name="mytcl.tcl"
tcl_script=split(realpath(argv[0]))[0] + "/" + script_name
root = Tk()
root.title("Example of running external process")
Button(root, text="Run Tcl script " + script_name, command=lambda: run_process(tcl_script,p)).pack()
root.geometry(str(root_w)+'x'+str(root_h)+'+'+str(root_x)+'+'+str(root_y))
root.mainloop()
#!/usr/bin/tcsh -f
set script_realpath=`realpath $0`
set script_path=$script_realpath:h
env PATH=${script_path}:/usr/bin:/bin pytclsub.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment