+ 1

why x still undefined?

from tkinter import * z= {'black':1, 'red':2,'yellow':3,'blue':4,'red':5} co=list(z) v=[] ls=[] w = Tk() g=Label(w,text=' ',width=10,height=5) g.pack() c=0 def f(): global x global c m=co[c%len(co)] g.config(bg=m) a=z.get(m) v.append(str(a)) w.after(1000,f) c+=1 if c==5 : g.pack_forget() x=''.join(v) f() b1=Button(w,text='',width=8,height=5,bg='black',command=lambda:ls.append(1)).pack(side=LEFT) b2=Button(w,text='',width=8,height=5,bg='red',command=lambda:ls.append(2)).pack(side=LEFT) b3=Button(w,text='',width=8,height=5,bg='yellow',command=lambda:ls.append(3)).pack(side=LEFT) b4=Button(w,text='',width=8,height=5,bg='blue',command=lambda:ls.append(4)).pack(side=LEFT) def check(): global x if ls==x: print(x) Button(w,text='check',command=check).pack() w.mainloop()

23rd Jan 2020, 3:37 PM
Pattern
Pattern - avatar
5 odpowiedzi
+ 1
What do I do to make x defined
23rd Jan 2020, 3:53 PM
Pattern
Pattern - avatar
+ 1
X =". Join(v) in the last line of f ()function I wanna use this value in check function how to do that
23rd Jan 2020, 4:28 PM
Pattern
Pattern - avatar
+ 1
Just declare global x x = 0 At the start of the file. Global functions inside functions can make changes to already globally declared variables.
23rd Jan 2020, 5:00 PM
XXX
XXX - avatar
0
the same way to define any variable ? What is “x” intended to be? there must be an x outside the scope.
23rd Jan 2020, 4:25 PM
Choe
Choe - avatar
0
ok. That declaration must be outside of f(). The use of global is to modify variables outside of the scope
23rd Jan 2020, 4:32 PM
Choe
Choe - avatar