在函数外部的函数内部使用相同的变量 - Dronekit - OpenCV

Using a the same variable inside functions outside a function - Dronekit - OpenCV

enter image description here

如何在主代码的函数中使用我定义的“red_position”变量?

你可以像这样在函数内部使用 global 关键字:

red_position = None

def yourfunction():
   your fucntion body 
   if ():
      global red_position # inside a if statement like in your code annd use it whereever you like
     

变量在 if 语句内,因此无法在函数外访问它,因此在 if 语句外声明变量。就这些

您可以在函数外部使用全局变量,并在调用函数时为其赋值。

您的函数也可以 return 值

return red_position

并像这样使用 returned_value = colour_detection()