I think you have the logic a little wrong. It comes from try to ‘patch’ the problem
Rather than using the ESC key to open a specific menu, maybe think of it as a ‘back’ button. To handle that, the ESC key needs an array of widgets.
When you press a menu button, the code closes ( or hides ) all widgets, and opens the next one, putting its reference on the end of the array.
When you use ESC, the code closes the current menu, and remove its reference. If the array is empty, go back to the game, otherwise show the next menu on the end of the array.
Like that…