-1

Im working on a Prgogramm with an GUI which stores, manipulate and analizes some Data. The data is stored in a json file like:

dict = {"team1_id":{"id1":id1,"id2":id2,"name":name},"team2_id":{"id1":id1,"id2":id2,"name":name}}

I have several functions which works with that data. For easier work i load the Data into 4 lists (internal_id_list,id1_list,id2_list,name_list) where each indice is pointing at the same team. Currently i open and load the data in every function new. That sounds bad in my opinion. A better way (which i couldnt figure out how to do) would be to load the data at the start, manipulate it while running (save it after each manipulate) and save it again when i close the script.

In my understandig of how (python) functions work, when they shall use informations from outside the function i need to give the function the information when i call it and when the function manipulate that informations and i wanna use it afterwards i need to return that informations. But how do i return that information when i call the function with a Button press with Tkinter? E.g.

import tkinter as tk
number = 1

def add():
    number += 1
    label.config(text=str(number))
    
def sub():
    number -= 1
    label.config(text=str(number))

root = tk.Tk()

label = tk.Label(root,text=str(number))
button_add = tk.Button(root,text="+ 1",command=add)
button_sub = tk.Button(root,text="- 1",command=sub)

label.pack()
button_add.pack()
button_sub.pack()

root.mainloop()

This script should increase or decreas a number and show it but it doesn't work because UnboundLocalError: local variable 'number' referenced before assignment Anyone who can explain this to me?

Edit: I found out that this would work:

import tkinter as tk
number = [1]

def add():
    number[0] = number[0]+1
    label.config(text=str(number[0]))
    
def subtract():
    number[0] = number[0]-1
    label.config(text=str(number[0]))

root = tk.Tk()

label = tk.Label(root,text=str(number[0]))
button_add = tk.Button(root,text="+ 1",command=add)
button_sub = tk.Button(root,text="- 1",command=subtract)

label.pack()
button_add.pack()
button_sub.pack()

root.mainloop()

But i dont understand the differnce why i dont need to use global this time.

1 Answer 1

1

The variable number cannot be reached inside your functions. One way would be to use global number in both functions to make that variable availabe inside the function.

"When you are in a function and assign a value to a variable, it assumes it is in the local scope. using global number tells that function to look to the global scope as the reference of number."*

def add():
    global number
    number += 1
    label.config(text=str(number))


def sub():
    global number
    number -= 1
    label.config(text=str(number))

source*: Python NameError, variable 'not defined'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.