所以我不断得到错误:
UnboundLocalError:赋值前引用的局部变量'old\u result'
我也不知道为什么。
我通常用javascript编写代码,所以我对python不太熟悉,但我也了解它,因为我在函数中再次声明了旧的结果,但我想使它成为全局的,只是在函数中更改它的值。反正我能修好这个
from PIL import Image, ImageGrab
import pytesseract
import threading
import winsound
old_result = 100
def ocrit():
threading.Timer(5.0, ocrit).start()
img = ImageGrab.grab(bbox=(110,70,250,200))
result = pytesseract.image_to_string(img,
lang='eng')
print(result)
new_result = int(result)
if new_result < old_result:
print("play da music")
old_result = new_result
ocrit()
如果要修改函数中全局变量的值,则必须在其中声明为全局变量。如果不修改全局变量的值,则可以将其声明为全局变量,也可以不声明,它将仅引用全局变量副本
因此,在访问全局变量之前,必须在函数中添加一行代码。因此,您的更新代码如下:
在函数中,声明:
相关问题 更多 >
编程相关推荐