Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run/kill a python script with another python script with help of EVDEV : bounce effect #232

Open
bbd666 opened this issue Jan 31, 2025 · 1 comment

Comments

@bbd666
Copy link

bbd666 commented Jan 31, 2025

this is a basic python script to draw a frame , toto.py

from tkinter import *
from tkinter import ttk
from tkinter import font as tkFont
from PIL import Image, ImageTk
import os

os.environ['DISPLAY'] = ':0'
decal_x=410
decal_y=360

root=Tk()
root.configure(bg='blue')
window_height=350
window_width=400
style_1 = ttk.Style()
style_1.configure('TFrame',background='yellow')    
screen_width=root.winfo_screenwidth()
screen_height=root.winfo_screenheight()
x_cordinate = int((screen_width/2) - (window_width/2))
y_cordinate = int((screen_height/2) - (window_height/2))
root.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate+40, y_cordinate))
content = ttk.Frame(root,style='new.TFrame')
content.place(x=decal_x, y=decal_y, anchor="se", width=window_width, height=window_height)

root.mainloop()

This a python script to run and kill (alternately) toto.py by typing 0-key of the remote ctrl

import time
import evdev
import os
import subprocess

    
def get_ir_device():
        devices = [evdev.InputDevice(path) for path in evdev.list_devices()]
        for device in devices:
            if (device.name == "gpio_ir_recv"):           
                return device          
   
    
def trig_ir():
    global last_call
    global s
    event = dev.read_one()
    result = None   
    if event is None:
        return
    else:    
     if not(event.code==0): 
      t = time.perf_counter() 
      if t - last_call > 0.5:
        print('ok')
        last_call = t
        value = event.value
        return value
                    
dev = get_ir_device()
process_pid=0
os.system('sh remote.sh')
last_call =time.perf_counter() 

while True:
    key=trig_ir()
    if (key==0):
       #print('0-key typed')
       if process_pid>0:
         os.system("pkill -f 'toto.py'")
         time.sleep(2)
         print('1 process killed')
         process_pid=0
       else:
         print('1 process launched')
         process = subprocess.Popen(["python", "toto.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
         process_pid=process.pid

when I press 0-key it runs toto.py
if I press again 0-key, very often (not always) it stops toto.py then it restarts toto.py
pls tell me what's wrong there

@bbd666
Copy link
Author

bbd666 commented Feb 1, 2025

This code works :

import evdev
import os
import subprocess
import psutil
    
def get_ir_device():
        devices = [evdev.InputDevice(path) for path in evdev.list_devices()]
        for device in devices:
            if (device.name == "gpio_ir_recv"):           
                return device          
   
    
def trig_ir():
    global last_call
    global s
    event = dev.read_one()
    result = None   
    if event is None:
        return
    else:    
     if not(event.code==0): 
      t = time.perf_counter() 
      if t - last_call > 0.5:
        last_call = t
        value = event.value
        return value
                    
dev = get_ir_device()
os.system('sh remote.sh')
last_call =time.perf_counter() 
processlist=list()
proc=['python','toto.py']

while True:
    key=trig_ir()
    if (key==0):
         processlist.clear()
         for process in psutil.process_iter():
           processlist.append(process.cmdline())
         if processlist.count(proc)==0:
           print('launching')
           process = subprocess.Popen(["python", "toto.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
         else:
          os.system("pkill -f 'toto.py'")  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant