I need a variable to send it by reference, then add a constant and the variable is updated with that value
interfaceLectorNotas.geometry("800x600")
lienzo = Canvas(interfaceLectorNotas, width=800, height=600)
lienzo.place(x=0, y=0)
# Se crean unas entradas por defecto
informacion = {"a.txt":"aaaa", "b.txt":"bbbb", "c.txt":"cccc", "d.txt":"dddd", "e.txt":"eeee", "f.txt":"ffff", "h.txt":"hhhh"}
contadorInformacion = 0
btnNextInformacion = Button(lienzo, text=">>", command= lambda cont=contadorInformacion : _subInterfaceNotasRefrescarInformacionSiguiente(informacion, cont))
btnNextInformacion.place(x=760, y=300)
btnPrevInformacion = Button(lienzo, text="<<", command= lambda cont=contadorInformacion : _subInterfaceNotasRefrescarInformacionAnterior(informacion, cont))
btnPrevInformacion.place(x=10, y=300)
#Al hacer esto:
#_subInterfaceNotasRefrescarInformacionSiguiente(informacion, cont):
#cont = contador + 1
#_subInterfaceNotasRefrescarInformacionAnterior(informacion, cont):
#cont = cont - 1
Counter does not save the increment or decrement
The thing is that I need counterInformacion to add 1????
How do I get the counter to save the new information????
In some languages, it is possible to modify the value of a variable external to a function by working on its reference instead of the value. That is why there is often talk of "call by reference" to a way in which this behavior is enabled in the functions.
In Python, it plays into the concept of "mutable" and "non-mutable" objects and numeric or string types, as is the variable in your question, are certainly "immutable" so even passing a reference (which is in fact what is passed in a function) it would be impossible to use it to modify the value.
What you are looking for, I understand, can be solved in several ways, which in How can I write a function without parameters (call by reference)? are well documented:
The most common: return and assign the new value:
The least recommended: use global variables
Use mutable variables: for example a list
Use a dictionary
Ultimately also a mutable object
Use an attribute of a class instance: