Defining functions with GUI buttons (Python\Tkinter) _beginner level_ (issue: "function; not definded) Defining functions with GUI buttons (Python\Tkinter) _beginner level_ (issue: "function; not definded) tkinter tkinter

Defining functions with GUI buttons (Python\Tkinter) _beginner level_ (issue: "function; not definded)


As per the advise of Stack Overflow user "Atlas435" I have simply moved the tk.Button line of code under the "def function" code. The interpreter reads code from top to bottom, so for the tk.Button(command=xyz) to work the interpreter first needs to read the function.

Code below:

def throwDice():    count = diceResult    while not count == 0:        input1 = int(input("guess a number 1,10 "))        randNum1 = random.randrange(1,10)    if (input1 == randNum1):        print("correct")        correct += 1                    else:        print ("incorrect")        tries += 1        print (randNum1)        print (count -1)        count -= 1        print ("computer", (tries) ,(userName),(correct))diceButton = tk.Button(window, text="roll dice", command=throwDice)#GUI enterGuessLabel = tk.Label(window, text="enter guess below")enterGuess =  tk.Entry(window)diceResultLabel = tk.Label(window, text="you rolled a: ")enterGuessLabel.pack()enterGuess.pack()diceButton.pack()diceResultLabel.pack()


Define the throwDice() function before defining the button

Because the python interpreter reads from the top