簡體   English   中英

為什么我的退出線程無法停止我的 while 循環?

[英]Why does my exit thread not work to stop my while loop?

所以這些代碼應該采用我提供的坐標,將它們隨機化,然后移動/單擊這些坐標並重復。 我想要一種退出 while(true) 循環的方法,所以我得到了線程代碼並使用了鍵“n”。 但它不起作用。

import keyboard
import time
import threading, pyautogui, sys, time, random, keyboard


class main:
    def __init__(self):
        # Create a run variable
        self.run = True

        # Start main thread and the break thread
        self.mainThread = threading.Thread(target=self.main)
        self.breakThread = threading.Thread(target=self.breakThread)

        self.mainThread.start()
        self.breakThread.start()

    def breakThread(self):
        # Check if run = True
        while True and self.run == True:
            if keyboard.is_pressed('n'):
                self.newFunction()

    def main(self):
        pyautogui.PAUSE = 0.01
        # Also check if run = True
        while not keyboard.is_pressed('n') and self.run == True:
            coords = [(327, 213),
                      (313, 309),
                      (307, 420),
                      (439, 420),
                      (561, 413),
                      (560, 320),
                      (428, 322),
                      (324, 198),
                      (437, 203),
                      (780,275)]
            random.shuffle(coords)
            for x in coords:
                pyautogui.moveTo(x[0], x[1], 0.01, pyautogui.easeInOutQuad)
                pyautogui.click()
                time.sleep(0.1)
            # Break like this
            if keyboard.is_pressed('n'):
                break

            time.sleep(0.1)

    def newFunction(self):
        self.run = False

program = main()

坦率地說,我不知道是什么問題,因為它適用於 Linux。

但我會展示一些不同的東西。 也許這對你有用。

keyboardkeyboard.add_hotkey('n', self.newFunction)不需要loop ,也不需要threading ,因為鍵盤將在自己的loop和自己的thread中運行它。 這樣,您也可以在沒有threading的情況下運行 function main()

import time
import random
import keyboard
import pyautogui


class Main:  # PEP8: `CamelCaseNames` for classes

    def __init__(self):
        self.run = True

        keyboard.add_hotkey('n', self.stop)

        self.main()

    def main(self):
        pyautogui.PAUSE = 0.01

        coords = [(327, 213),
                  (313, 309),
                  (307, 420),
                  (439, 420),
                  (561, 413),
                  (560, 320),
                  (428, 322),
                  (324, 198),
                  (437, 203),
                  (780, 275)]

        while self.run:
            random.shuffle(coords)

            for x, y in coords:
                pyautogui.moveTo(x, y, 0.01, pyautogui.easeInOutQuad)
                pyautogui.click()
                time.sleep(0.1)
                if not self.run:
                    #break  # exit only `for`-loop (and it will have to check `while self.run`)
                    return  # directly exit function `main()` 

    def stop(self):
        self.run = False

program = Main()

PEP 8 -- Python 代碼的樣式指南

你沒有退出 while 循環。

為此,只需在 if 之后包含 break

    def breakThread(self):
        # Check if run = True
        while True and self.run == True:
            if keyboard.is_pressed('n'):
                self.run = False
                break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM