1

I am learning python on my own and my level is probably a poor excuse for a "script kiddie" as I kinda understand and mostly end up borrowing and mashing together different scripts till it does what I want. However this is the first time I'm trying to create a GUI for one of the scripts I have. I'm using PySimpleGUI and I've been able to understand it surprisingly well. All but one thing is working the way I want it to.

The issue is I want to stop a running daemon thread without exiting the GUI. If I get the stop button to work the GUI closes and if the GUI does not close it doesn't stop the thread. The issue is between lines '64-68'. I have tried a few things and just put a place holder on line '65' to remember that I was trying to keep the GUI ("Main Thread" in my head-speak) running. The script will run in this state but the 'Stop' button does not work.

Note: I put a lot of comments in my scripts so I remember what each part is, what it does and what I need to clean up. I don't know if this is a good practice if I plan on sharing a script. Also, if it matters, I use Visual Studio Code.

#!/usr/local/bin/python3

import PySimpleGUI as sg
import pyautogui
import queue
import threading
import time
import sys
from datetime import datetime
from idlelib import window

pyautogui.FAILSAFE = False
numMin = None

# ------------------ Thread ---------------------

def move_cursor(gui_queue):
    if ((len(sys.argv)<2) or sys.argv[1].isalpha() or int(sys.argv[1])<1):
        numMin = 3
    else:
        numMin = int(sys.argv[1])
    while(True):
        x=0
        while(x<numMin):
            time.sleep(5)                   # Set short for debugging (will set to '60' later)
            x+=1
        for i in range(0,50):
            pyautogui.moveTo(0,i*4)
        pyautogui.moveTo(1,1)
        for i in range(0,3):
            pyautogui.press("shift")
        print("Movement made at {}".format(datetime.now().time()))  

# --------------------- GUI ---------------------

def the_gui():
    sg.theme('LightGrey1')                  # Add a touch of color
    gui_queue = queue.Queue()               # Used to communicate between GUI and thread


    layout = [  [sg.Text('Execution Log')],
             [sg.Output(size=(30, 6))],
             [sg.Button('Start'), sg.Button('Stop'), sg.Button('Click Me'), sg.Button('Close')]  ]
    window = sg.Window('Stay Available', layout)

# -------------- EVENT LOOP ---------------------
    # Event Loop to process "events"

    while True:
        event, values = window.read(timeout=100)
        if event in (None,'Close'):
          break
        elif event.startswith('Start'):     # Start button event
            try:
                print('Starting "Stay Available" app')
                threading.Thread(target=move_cursor,
                                 args=(gui_queue,), daemon=True).start()
            except queue.Empty:
                print('App did not run')
        elif event.startswith('Stop'):      # Stop button event
            try:
                print('Stopping "Stay Available" app')
                threading.main_thread       # To remind me I want to go back to the original state
            except queue.Empty:
                print('App did not stop')
        elif event == 'Click Me':           # To see if GUI is responding (will be removed later)
            print('Your GUI is alive and well')

    window.close(); del window

if __name__ == '__main__':
    gui_queue = queue.Queue()               # Not sure if it goes here or where it is above
    the_gui()
    print('Exiting Program')
Prylux
  • 11
  • 4
  • The `while True:` loop could rather be a `while running:`, where `running` is initially `True` and you set it to `False` when the thread is supposed to shut down. – tevemadar Apr 10 '20 at 22:56

1 Answers1

0

From this answer: create the class stoppable_thread.

Then: store the threads on a global variable:

# [...]
# store the threads on a global variable or somewhere

all_threads = []

# Create the function that will send events to the ui loop

def start_reading(window, sudo_password = ""):
    While True:
        window.write_event_value('-THREAD-', 'event')
        time.sleep(.5)
        
# Create start and stop threads function

def start_thread(window):
    t1 = Stoppable_Thread(target=start_reading,  args=(window,), daemon=True)
    t1.start()
    all_threads.append(t1)

def stop_all_threads():
    for thread in all_threads:
        thread.terminate()

Finally, on the main window loop, handle the events that start, stop or get information from the thread.

selan
  • 956
  • 3
  • 11
  • 27