简体   繁体   English

线程中的 PIL.ImageGrab、grab() 和 load() 函数

[英]PIL.ImageGrab , grab() and load() functions in thread

I want to create a game bot but with threading.我想创建一个带有线程的游戏机器人。

When script founds game screen works 1 time and stop with this message "Process finished with exit code 0".当脚本发现游戏屏幕工作 1 次并停止并显示此消息“进程已完成,退出代码为 0”。 But scrpit can not found the game screen, works truly...但是scrpit找不到游戏画面,确实有效...

How can I fix this problem?我该如何解决这个问题? Thanks a lot.非常感谢。

class POT(Thread):
    global x, y


    def __init__(self):
        Thread.__init__(self)
        self.running = True

    def run(self):
        while self.running:
            try:
                x, y = locateCenterOnScreen('menu.PNG')
                text = grab().load()[x - 670, y - 730]
                text2 = grab().load()[x - 757, y - 730]
                if (text[0] < 150):
                    for z in range(3):
                        press('0')
                        sleep(0.05)
                        release('0')
                        sleep(0.05)
                    print('HP USED')

                    if text2[0] < 150:
                        for z in range(3):
                            press('6')
                            sleep(0.025)
                            release('6')
                            sleep(0.025)
                            press('5')
                            sleep(0.033)
                            release('5')
                            sleep(0.033)
                        print('INTUITION USED 6')
                        print('AURA USED 5')
                print(x, y)
                return x, y
            except:
                print("GAME SCREEN NOT FOUND...")



bot=POT()
bot.start()
bot.join()

You're doing return x, y .你正在做return x, y

That will break the while self.running loop, so the thread ends, and with it, your program.这将打破while self.running循环,因此线程结束,您的程序也随之结束。

As an aside:作为旁白:

  • you don't need a thread for this since your program is doing nothing else than this loop.你不需要线程,因为你的程序除了这个循环之外什么都不做。
  • you should never, ever use a bare try: except: block.你永远不应该使用一个简单的try: except:块。 It will also catch SystemExit s and KeyboardInterrupt s.它还将捕获SystemExitKeyboardInterrupt
    • If you'd use try: except Exception: instead of try: except: , you'd want to use traceback.print_exc() so you know the actual exception.如果你使用try: except Exception:而不是try: except: ,你会想要使用traceback.print_exc()来了解实际的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何在PIL.ImageGrab中获取屏幕截图数据? - Python:How can I get screenshot data in PIL.ImageGrab? PIL imagegrab().grab().crop(...) 作物不是给定的坐标 - PIL imagegrab().grab().crop(...) crops not the given coordinates 使用 PIL.ImageGrab 和 cv2.imread 时如何强制执行 alpha 通道? - How to enforce alpha channel when using PIL.ImageGrab and cv2.imread? 捕获屏幕有比PIL.ImageGrab.grab()更好的方法吗? - Is there any better way to capture the screen than PIL.ImageGrab.grab()? RGB是从ImageGrab获取的吗。grab()。load()是数组还是字符串 - Is RGB taken from ImageGrab.grab().load() is in array or string PIL的.crop返回图像0x0。 指定bbox的ImageGrab.grab也是如此 - PIL's .crop returns image 0x0. So does ImageGrab.grab with specified bbox 为什么特定窗口的 PIL.ImageGrab.grab 无法正常工作? - Why does PIL.ImageGrab.grab of specific window doesn't work properly? 如何从 Python 中的 PIL.ImageGrab.grab() 获取实际像素颜色值? - How to get actual Pixel Color value from PIL.ImageGrab.grab() in Python? 有什么办法可以让 PIL 的 ImageGrab.grab() 保存小尺寸的照片? - Is there any way to make PIL's ImageGrab.grab() save photos with a small size? 屏幕截图(PIL)Win10上的ImageGrab.grab / BitBlt仅返回背景吗? - Screenshot (PIL)ImageGrab.grab / BitBlt on Win10 only return the Background?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM