Title: Defining functions with GUI buttons (Python\Tkinter) _beginner level_ (issue: "function; not definded)
Tags: python;tkinter;notepad
Question: Currently studying Python for class, I am creating a number guessing game.
objectives of code:

a user rolls a dice, that determines how many attempts they get to guess a number between 1 to 100
results are saved in a notepad.

I am able to get the guessing game working in shell. However, i am trying to convert game to GUI.
CURRENT ISSUE
I have assigned a tk.Button to run function when pressed. However, I am receiving  an error.
Code below;
```import random
import tkinter as tk
import time
import sys
window = tk.Tk()
window.title(&quot;Shanes Number Guessing Game&quot;)
window.geometry(&quot;900x500&quot;)
diceResult = random.randrange(1,6)
print (diceResult)
tries= 0
correct = 0

   
logo = tk.PhotoImage(file=&quot;C:\Python-Tkinter pics\\numberguess.png&quot;)
photo1 = tk.Label(image=logo)
photo1.image = logo
photo1.pack()

#gui buttons and lables

enterGuessLabel = tk.Label(window, text=&quot;enter guess below&quot;)
enterGuess =  tk.Entry(window)
diceButton = tk.Button(window, text=&quot;roll dice&quot;, command=throwDice)
diceResultLabel = tk.Label(window, text=&quot;you rolled a: &quot;)


#Number guess code


def throwDice():
    count = diceResult
    while not count == 0:
        input1 = int(input(&quot;guess a number 1,10 &quot;))
        randNum1 = random.randrange(1,10)

    if (input1 == randNum1):
        print(&quot;correct&quot;)
        correct += 1
                
    else:
        print (&quot;incorrect&quot;)
        tries += 1
        print (randNum1)
        print (count -1)
        count -= 1
        print (&quot;computer&quot;, (tries) ,(userName),(correct))



#GUI pack

enterGuessLabel.pack()
enterGuess.pack()
diceButton.pack()
diceResultLabel.pack()
window.mainpack()
```
Here is the output
```    5
name 'throwDice' is not defined
Stack trace:
 &gt;  File &quot;C:\Users\shane\source\repos\ICT30118 CertIII Assessment\ICT30118 CertIII Assessment\ICT30118_CertIII_Assessment.py&quot;, line 31, in <module&gt;
 &gt;    diceButton = tk.Button(window, text=&quot;roll dice&quot;, command=throwDice)
Loaded '__main__'
Loaded 'runpy'
```
I have assigned a &quot;tk.Button&quot; to run the command &quot;throw dice&quot; when pressed.
I have assigned a &quot;def function&quot; as &quot;throwDice():&quot;
When I run the program, I receive an error that &quot;throwDice&quot; is not defined
Any help will be appreciated.

Comment: I am unsure of how to define a function with the same name, i have attempted to use the global function. will post results in main post.

Comment: It worked, I will edit post, thanks.

Comment: You need to define your function before you use it as a command. Usually you define your global function right after you imported your stuff. Keep in mind that your interpreter reads your code left to right and up to down.

Comment: Also take a look at this https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions

Comment: I see I had confused you. What I mean by a global function is that your function is in the global namespace. You dont need to define that function twice, you need to place it at the right spot. Again your Intepreter reads your code up to down. Your function is on the very bottom of your code and your button is in the middle. How should your Interpreter know your funtion at this point? Link for global namespace https://maria33@example.org/understanding-python-namespaces-through-recursion-c921cbd4e522

Comment: Congratulations! :)

Here is the accepted answer: Define the throwDice() function before defining the button
Because the python interpreter reads from the top


Here is another answer: As per the advise of Stack Overflow user  &quot;Atlas435&quot; I have simply moved the tk.Button line of code under the &quot;def function&quot; 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(&quot;guess a number 1,10 &quot;))
        randNum1 = random.randrange(1,10)

    if (input1 == randNum1):
        print(&quot;correct&quot;)
        correct += 1
                
    else:
        print (&quot;incorrect&quot;)
        tries += 1
        print (randNum1)
        print (count -1)
        count -= 1
        print (&quot;computer&quot;, (tries) ,(userName),(correct))

diceButton = tk.Button(window, text=&quot;roll dice&quot;, command=throwDice)

#GUI 
enterGuessLabel = tk.Label(window, text=&quot;enter guess below&quot;)
enterGuess =  tk.Entry(window)
diceResultLabel = tk.Label(window, text=&quot;you rolled a: &quot;)
enterGuessLabel.pack()
enterGuess.pack()
diceButton.pack()
diceResultLabel.pack()
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:08/08/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><tkinter><notepad>', 'Title': 'Defining functions with GUI buttons (Python\\Tkinter) _beginner level_ (issue: "function; not definded)', 'ViewCount': 63, 'id': 63314349, 'user_AboutMe': None, 'user_AccountId': None, 'user_DisplayName': None, 'user_Reputation': None, 'user_Views': None, 'user_id': None}}