1
votes

I am in the process of converting a shell only code to GUI.

My issue is that I have defined a variable within one def function and I want to run this variable in a second def function. I have attempted to make the variable global however I am still getting an error.

Below in include my code; (please note the two def functions)

import tkinter as tk
import time
import random
import sys
window = tk.Tk()
window.title("Shanes Number Guessing Game")
window.geometry("600x500")

#GUI Image
logo = tk.PhotoImage(file="C:\Python-Tkinter pics\\numberguess.png")
photo1 = tk.Label(image=logo)
photo1.image = logo
photo1.pack()

#score
tries = 0
wins = 0


#enter username
userNameLabel = tk.Label(window,text="please enter your name below")
userNameEntry = tk.Entry(window)
userNameLabel.pack()
userNameEntry.pack()


#GUI Buttons
enterGuessLabel = tk.Label(window, text="enter guess below")
enterGuessLabel.pack()

enterGuess =  tk.Entry(window)
guess = enterGuess
enterGuess.pack()


#random number 
rannum = random.randrange(1,100)
print (rannum)

#Throw dice
def throwDice():
    global diceResult
    global tries
    diceResult = random.randrange(1,6)
    print (diceResult)
    tries += diceResult                 #UnboundLocalError: local variable 'tries' referenced before assignment

                                        #diceResult is in a function
def takeGuess():
    global wins
    global losses
    global tries
    global diceResult
    count = diceResult   #NameError: name 'diceResult' is not defined
    while not count == 0:
        tries = diceResult  #NameError: name 'diceResult' is not defined
        if (enterGuess == rannum):
            print ("correct")
            wins += 1
            print (win)
            break
    
        else:
            print("incorrect")
            tries += 1
            count -= 1
                      

    #GUI Buttons
diceButton = tk.Button(window, text="roll dice", command=throwDice)
diceButton.pack()

guessButton = tk.Button(window, text="take guess", command=takeGuess)  #button will need a comand
inputGuess = guessButton
guessButton.pack()


window.mainloop()

When i action the program, the GUI opens. When I click on the GUI button called "diceButton" (that runs the command" throwdice") I gain the expected results in the shell.

However, when I click on the GUI "guessButton" (that runs the command "takeGuess") i receive the error "NameError: name 'diceResult' is not defined"

Shell output:

10
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\shane\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:\Users\shane\source\repos\ICT30118 CertIII Assessment\ICT30118 CertIII Assessment\ICT30118_CertIII_Assessment.py", line 54, in takeGuess
    count = diceResult   #NameError: name 'diceResult' is not defined
NameError: name 'diceResult' is not defined

I have attempted to do some research on this issue, however i am still confused. https://medium.com/@vbsreddy1/unboundlocalerror-when-the-variable-has-a-value-in-python-e34e097547d6#:~:text=UnboundLocalError%3A%20local%20variable%20referenced%20before%20assignment%20in%20Python.,-VINTA%20BHARATH%20SAI&text=In%20Python%2C%20variables%20that%20are,unless%20explicitly%20declared%20as%20global.

1

1 Answers

2
votes

diceResult isn't defined until you call throwDice. If you click on the "guess" button before clicking on the "roll dice" button, diceResult won't exist and you get the error in your question.

In other words, the problem isn't in how you define the variables as global, but rather when the variables are initially defined.