Python 无递归限制的无限屏幕检测

Python 无递归限制的无限屏幕检测,python,recursion,tkinter,python-imaging-library,Python,Recursion,Tkinter,Python Imaging Library,我已经编写了一个图像检测程序,可以截取屏幕截图,检测特定的图像,进行计算,并在Tkinter窗口中向用户报告。除了我需要它一直运行直到用户退出程序之外,它一切正常,但是我达到了递归限制(1000)并得到了错误RecursionError:maximum recursion depth extended。如果我处理这个错误,当然最终会导致堆栈溢出错误 我的方法显然有一些根本性的缺陷。我可以用什么方法无限地拍摄屏幕截图,处理输出并在Tkinter窗口中向用户报告 我的精简方法如下。提前感谢您的任何建

我已经编写了一个图像检测程序,可以截取屏幕截图,检测特定的图像,进行计算,并在Tkinter窗口中向用户报告。除了我需要它一直运行直到用户退出程序之外,它一切正常,但是我达到了递归限制(1000)并得到了错误
RecursionError:maximum recursion depth extended
。如果我处理这个错误,当然最终会导致
堆栈溢出错误

我的方法显然有一些根本性的缺陷。我可以用什么方法无限地拍摄屏幕截图,处理输出并在Tkinter窗口中向用户报告

我的精简方法如下。提前感谢您的任何建议或想法

from PIL import ImageGrab
import tkinter as tk

class Main(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.screen_height = self.winfo_screenheight()
        self.screen_width = self.winfo_screenwidth()
        self.callback_time = 1 # Actual time is 1000
        self.num_screenshots = 0
        self.wm_attributes("-topmost", 1) # Keeps the Tkinter window above other windows
        tk.Label(text="Number of Screenshots").pack(side="left")
        self.counter_label = tk.Label()
        self.counter_label.pack(side="left")

    def process_screen(self):
        # Do shape detection here
        self.num_screenshots += 1
        self.counter_label.configure(text=self.num_screenshots)
        self.update_idletasks()

    def take_screen_shot(self):
        #self.screen_shot = ImageGrab.grab(bbox=(0, 0, self.screen_width, self.screen_height)) 
        self.process_screen()
        self.after(self.callback_time, self.take_screen_shot())

    def run(self):
        def callback(event):
            if True:
                self.after_cancel(self.process)
                print("Call back Cancelled")
        self.bind("<Return>", callback)
        self.process = self.after(self.callback_time, self.take_screen_shot)
        self.mainloop()

if __name__ == "__main__":
    Main().run()
从PIL导入ImageGrab
将tkinter作为tk导入
主类(tk.tk):
定义初始化(self,*args,**kwargs):
tk.tk.\uuuuu初始化(self,*args,**kwargs)
self.screen_height=self.winfo_screen height()
self.screen_width=self.winfo_screenwidth()
self.callback_time=1#实际时间为1000
self.num_屏幕截图=0
self.wm_属性(“-toppost”,1)#将Tkinter窗口保持在其他窗口之上
tk.Label(text=“屏幕截图数”).pack(side=“left”)
self.counter_label=tk.label()
自助柜台标签包(side=“left”)
def过程_屏幕(自身):
#在这里做形状检测
self.num_屏幕截图+=1
self.counter\u label.configure(text=self.num\u屏幕截图)
self.update_idletasks()
def拍摄屏幕快照(自身):
#self.screen\u shot=ImageGrab.grab(bbox=(0,0,self.screen\u宽度,self.screen\u高度))
self.process_screen()
self.after(self.callback\u time,self.take\u screen\u shot())
def运行(自):
def回调(事件):
如果为真:
取消后的自我(自我处理)
打印(“取消回拨”)
self.bind(“,回调)
self.process=self.after(self.callback\u time,self.take\u screen\u shot)
self.mainloop()
如果名称=“\uuuuu main\uuuuuuuu”:
Main().run()

新手犯的错误
self.after(self.callback\u time,self.take\u screen\u shot())
应该是
self.after(self.callback\u time,self.take\u screen\u shot)
不带括号。

什么是主循环,为什么不在无限循环中调用所有内容?你不能在True:screenshot()sleep(5000)时用
启动一个线程吗
还是类似的?