Mr G Clapton
Mr G Clapton

Reputation: 13

TypeError: 'StringVar' object is not iterable

from tkinter import*
raiz=Tk()
raiz.title("Last Recently Used LRU")
raiz.resizable(1,1)
raiz.geometry("1080x720")
#-----------------------------------------
marcos=IntVar()
#-------------------------------------
label1=Label(raiz,text="Numero de Marcos:")
label1.place(x=260,y=100)
texto1=Entry(raiz,textvariable=marcos)
texto1.place(x=500,y=100)
s=StringVar()
label2_5=Label(raiz,text="*Introduce una cadena de numeros separados por espacios")
label2_5.place(x=260,y=200)
label2=Label(raiz,text="Cadena de Referencias:")
label2.place(x=260,y=250)
texto2=Entry(raiz,textvariable=s)
texto2.place(x=555,y=250)
def perro():
    PROC=IntVar()
    PROC = int(input())
    f, st, fallos, mf = [], [], 0, 'No'
    s = list(map(int, input().strip().split()))
for i in s:
    if i not in f:
        if len(f)<PROC:
            f.append(i)
            st.append(len(f)-1)
        else:
            ind = st.pop(0)
            f[ind] = i
            st.append(ind)
        mf = 'X'
        fallos += 1
    else:
        st.append(st.pop(st.index(f.index(i))))
    mf = '--'
    print("\n\n")
    print("   %d\t\t" % i, end='')
    for x in f:
        print(x, end=' ')
    for x in range(PROC - len(f)):
        print(' ', end=' ')
    print(" %s" % mf)
botonp=Button(raiz,text="Ejecutar",command=perro)


botonp.place(x=540,y=350)
raiz.mainloop()

line 33, in for i in s: TypeError: 'StringVar' object is not iterable

Ok here is the full code, i try to make a GUI with Tkinter but there's a problem, i don't know what to do with this error.

Any ideas how to fix?

Upvotes: 0

Views: 1614

Answers (1)

blhsing
blhsing

Reputation: 107085

The name s is defined as a global variable of type StringVar with the statement:

s=StringVar()

so when you attempt to iterate over it with:

for i in s:

it produces the said exception since your StringVar object is not iterable.

The fact that you assigned s with a list inside the function perro does not help because the s variable that is assigned with a list is local to the perro function and is not at all the same s as the global variable.

You should make perro return the list and iterate over the returning value instead.

Change:

    s = list(map(int, input().strip().split()))
for i in s:

to:

    return list(map(int, input().strip().split()))
for i in perro():

Upvotes: 1

Related Questions