0

I want to be able to access difficulty and startingRegion inside the validateStart() function. How do I do this? I am using tkinter

def startgame():
    root = Tk()

    #storing user choice under variable difficulty
    difficulty = StringVar(value="Easy")
    difDropdown = OptionMenu(root, difficulty, "Easy", "Hard")

    regions = []
  
    startingRegion = StringVar()
    startingRegion.set(regions[0])
    difDropdown = OptionMenu(root, startingRegion, *regions)
    
    def validateStart():
        global root
        print(startingRegion)
        print(difficulty)
        root.destroy()
    
    #button which when clicked goes to validateStart()
    playGameButton = Button(command = validateStart)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • You only need to declare a variable as `global` in functions which explicit assign new values to them (to it being treated as a local variable definition). That said, I don't really understand what you're trying to do. Please [edit] your question and provide a runnable [mre] illustrating the problem. – martineau Jan 16 '21 at 18:32
  • I'm basically designing a game, and in the Tk() not indented, I have assigned variables for the user to choose. It's essentially a menu window to choose the settings of the game. I then want to access these variables inside the new validateStart() function, which is where they will be required in a new window, where the game is played. But in order to play the game, these two variables are required – Aran Khalastchi Jan 16 '21 at 18:42
  • The only function in your current code is `validateStart()` and it doesn't access any of the `global` variables — it just declares them as such (which as I mentioned, isn't necessary unless new values are being assigned to them, which is not the case as shown. Please update your question as I suggested. – martineau Jan 16 '21 at 18:47
  • I've edited it. Hopefully I've made it a lot clearer than it was before – Aran Khalastchi Jan 16 '21 at 19:04
  • Well, it's a little more clear…enough for me to see that `validateStart()` is a nested function and that `root` is *not** actually a global variable, it's variable local to `startgame()`. To access it in validateStart()` declare it `nonlocal root`. (See [`nonlocal`](https://docs.python.org/3/reference/simple_stmts.html#the-nonlocal-statement) in the documentation. – martineau Jan 16 '21 at 19:18
  • Thank you so much for your help. I've never used nonlocal roots before (only starting to get into code), so I didn't even know this was a thing. Also thank you for your patience. It's my first time putting code on here so the help is very much appreciated! – Aran Khalastchi Jan 16 '21 at 19:55
  • You're welcome…nice to hear you found my comments helpful. Good luck with your game. – martineau Jan 16 '21 at 21:06
  • P.S. For the record, another way to fix the problem would be to declare `global root` at the beginning of `startgame()` too, thus actually making it a global (and leaving it declared that way in `validateStart()`). – martineau Jan 17 '21 at 06:29

0 Answers0