0

I want to add a command that when the user presses any/any specific key, the program will stop running. I am unsure how to add this into a program that will likely loop over 500 times when in use. Not sure if the context of the code is helpful but I added it below anyway. I'm fairly new to Python/this type of coding so I know my code is messy and likely not very efficient but the program works bar the keystroke break switch. Please help.

# Import necessary libraries
import pyautogui as py
import pandas as pd
import time


# Function loop to check when print load screen is complete
def loading():

    check = py.getActiveWindowTitle()

    while check != *Title*:

        time.sleep(0.3)
        check = py.getActiveWindowTitle()

        # Print is done break the loop
        if check == *Title*:
            break


# Function definition for loop performing repetitive clicking
def auto():

    # Initialize variables
    i = 1

    # Initial move to 'Print'
    py.moveTo(x, y, duration=0.1)

    # Loop through printing sequence
    while i <= endNum:

        # Calculates values from csv to compare to limits
        LTD = int(abs(res.iloc[i-1, 0]) * 10)
        MTD = int(abs(res.iloc[i-1, 1]) * 10)
        HTD = int(abs(res.iloc[i-1, 2]) * 10)
        LHD = int(abs(res.iloc[i-1, 3]) * 10)
        MHD = int(abs(res.iloc[i-1, 4]) * 10)
        HHD = int(abs(res.iloc[i-1, 5]) * 10)

        # Determines if any device is out of spec and skips print to dismiss
        if LTD > 20 or MTD > 20 or HTD > 20 or LHD > 20 or MHD > 20 or HHD > 20:

            # Move to/click 'Okay' to dismiss out of spec notice
            py.moveTo(m, n, duration=0.2)
            py.click(m, n, duration=0.1)
            time.sleep(0.25)
            py.click(m, n, duration=0.1)

            # Move to/click 'Next' to continue printing
            py.moveTo(a, b, duration=0.2)
            py.click(a, b, duration=0.1)

            i = i + 1

        # Click 'Print'
        py.click(x, y, duration=0.1)
        time.sleep(0.75)

        # Create variable for possible blank page notification
        blank = py.getActiveWindowTitle()

        # If blank page IS NOT present
        if blank != *Title*:

            # Call function to determine when done printing
            loading()

            # Move/Click 'Next'
            py.moveTo(a, b, duration=0.2)
            py.click(a, b, duration=0.1)

            # Move back to 'Print' to repeat the process
            py.moveTo(x, y, duration=0.2)

            i = i+1

        # If blank page IS present
        if blank == *Title*:

            # Move cursor to dismiss blank page printing window
            py.moveTo(j, k, duration=0.2)
            py.click(j, k, duration=0.1)

            # Call function to determine when done printing
            loading()

            # Move/Click 'Next'
            py.moveTo(a, b, duration=0.2)
            py.click(a, b, duration=0.1)

            # Move back to 'Print' to repeat the process
            py.moveTo(x, y, duration=0.2)

            i += 1


# Open results file and get length of file
# for number of  to be printed
res = pd.read_csv('csv filename', usecols=[4, 7, 10, 14, 17, 20])

# Number of  to be printed
endNum = len(res)

# Delay for user to move cursor to correct position/monitor
time.sleep(10)

# Determine screen resolution
(w, h) = py.size()

# Different functions based on screen resolution

# If screen res = 3000 x 2000
if (w, h) == (3000, 2000):

    # Pixel coordinates for 3000x2000 click locations
    x = 1243
    y = 737
    a = 1097
    b = 811
    j = 1445
    k = 1108
    m = 1668
    n = 1118

    # Call looping function
    auto()

# If screen res = 1920 x 1080
if (w, h) == (1920, 1080):

    # Pixel coordinates for 1920x1080 click locations
    x = 1033
    y = 523
    a = 958
    b = 559
    j = 925
    k = 590
    m = 1053
    n = 602

    # Call looping function
    auto()
  • Try looking at [How to stop a function & exit program running in a while loop on key press?](https://stackoverflow.com/questions/70460790/how-to-stop-a-function-exit-program-running-in-a-while-loop-on-key-press) – Alias Cartellano Sep 30 '22 at 16:13

0 Answers0