0

У меня есть цикл While и мне нужно что-бы когда открывалась функция, этот цикл завершался.

Пример:

def exit():
     #код который завершает цикл в функции run

def run():
    global time,delay,x
    delay.configure(state='disabled')
    #time.configure(state='disabled')

    start.grid_remove()
    stop.grid(column=0, row=2)

    delay = int(delay.get())
    #time = int(time.get())


    while x!=1:
        pyautogui.PAUSE = delay
        pyautogui.click(clicks=1)
        keyboard.add_hotkey('e', exit)
1
  • def exit(): x = 1 или def exit(): global x x = 1
    – Sergey
    12 июн 2020 в 2:44

1 ответ 1

0
def exit():
     #код который завершает цикл в функции run
     raise KeyboardInterrupt()

def run():
    global time,delay,x
    delay.configure(state='disabled')
    #time.configure(state='disabled')

    start.grid_remove()
    stop.grid(column=0, row=2)

    delay = int(delay.get())
    #time = int(time.get())

    try:
        while x!=1:
          pyautogui.PAUSE = delay
          pyautogui.click(clicks=1)
          keyboard.add_hotkey('e', exit)
    except KeyboardInterrupt:
        pass
    # код дальше
2
  • в консоле выдает ошибку, а программа сама не завершается @eri
    – user383587
    12 июн 2020 в 0:41
  • попробуй raise Exception() но похоже библиотека перехвативает. надо смотреть стек ошибки
    – eri
    12 июн 2020 в 0:44

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.