Hello!
Im beginner at Tkinter and this is my 1st project. I want make programm that calculate resistor value from colors, but i can`t get variables out of functions. Here you can see all code: #fails 251.py import Tkinter as tk from Tkinter import * import math root=tk.Tk() root.title("rezistoru aprekins") w= tk.Canvas(root, width=700, height=550, bg="#abc") w.pack() linija = w.create_line(10, 100, 375, 100, width='10',fill='black') linija = w.create_line(100, 50, 100, 150, width='50',fill='#FFCC99') linija = w.create_line(300, 50, 300, 150, width='50',fill='#FFCC99') linija = w.create_line(75, 100, 300, 100, width='80',fill='#FFCC99') linija = w.create_line(110, 50, 110, 150, width='15',fill='red') linija = w.create_line(150, 60, 150, 140, width='15',fill='red') linija = w.create_line(180, 60, 180, 140, width='15',fill='red') linija = w.create_line(260, 60, 260, 140, width='15',fill='red') linija = w.create_line(30, 190, 110, 155, width='1',fill='black') linija = w.create_line(150, 148, 200, 190, width='1',fill='black') linija = w.create_line(180, 145, 350, 190, width='1',fill='black') linija = w.create_line(260, 145, 500, 190, width='1',fill='black') #x1=tk.IntVar() #x2=tk.IntVar() def fun2(): linija = w.create_line(110, 50, 110, 150, width='15',fill='black') x=00 w.itemconfig(t2,text=x) def fun3(): linija = w.create_line(110, 50, 110, 150, width='15',fill='brown') x=10 w.itemconfig(t2,text=x) def fun4(): linija = w.create_line(110, 50, 110, 150, width='15',fill='red') x=20 w.itemconfig(t2,text=x) def fun5(): linija = w.create_line(110, 50, 110, 150, width='15',fill='orange') x=30 w.itemconfig(t2,text=x) def fun6(): linija = w.create_line(110, 50, 110, 150, width='15',fill='yellow') x=40 w.itemconfig(t2,text=x) def fun7(): linija = w.create_line(110, 50, 110, 150, width='15',fill='green') x=50 w.itemconfig(t2,text=x) def fun8(): linija = w.create_line(110, 50, 110, 150, width='15',fill='blue') x=60 w.itemconfig(t2,text=x) def fun9(): linija = w.create_line(110, 50, 110, 150, width='15',fill='purple') x=70 w.itemconfig(t2,text=x) def fun10(): linija = w.create_line(110, 50, 110, 150, width='15',fill='gray') x=80 w.itemconfig(t2,text=x) def fun11(): linija = w.create_line(110, 50, 110, 150, width='15',fill='white') x=90 w.itemconfig(t2,text=x) def fun21(): linija = w.create_line(150, 60, 150, 140, width='15',fill='black') x1=00 w.itemconfig(t3,text=x1) def fun31(): linija = w.create_line(150, 60, 150, 140, width='15',fill='brown') x1=1 w.itemconfig(t3,text=x1) def fun41(): linija = w.create_line(150, 60, 150, 140, width='15',fill='red') x1=2 w.itemconfig(t3,text=x1) def fun51(): linija = w.create_line(150, 60, 150, 140, width='15',fill='orange') x1=3 w.itemconfig(t3,text=x1) def fun61(): linija = w.create_line(150, 60, 150, 140, width='15',fill='yellow') x1=4 w.itemconfig(t3,text=x1) def fun71(): linija = w.create_line(150, 60, 150, 140, width='15',fill='green') x1=5 w.itemconfig(t3,text=x1) def fun81(): linija = w.create_line(150, 60, 150, 140, width='15',fill='blue') x1=6 w.itemconfig(t3,text=x1) def fun91(): linija = w.create_line(150, 60, 150, 140, width='15',fill='purple') x1=7 w.itemconfig(t3,text=x1) def fun101(): linija = w.create_line(150, 60, 150, 140, width='15',fill='gray') x1=8 w.itemconfig(t3,text=x1) def fun111(): linija = w.create_line(150, 60, 150, 140, width='15',fill='white') x1=9 w.itemconfig(t3,text=x1) #3linija def fun22(): linija = w.create_line(180, 60, 180, 140, width='15',fill='black') x2=1 w.itemconfig(t4,text=x2) def fun32(): linija = w.create_line(180, 60, 180, 140, width='15',fill='brown') x2=10 w.itemconfig(t4,text=x2) def fun42(): linija = w.create_line(180, 60, 180, 140, width='15',fill='red') x2=100 w.itemconfig(t4,text=x2) def fun52(): linija = w.create_line(180, 60, 180, 140, width='15',fill='orange') x2=1000 w.itemconfig(t4,text=x2) def fun62(): linija = w.create_line(180, 60, 180, 140, width='15',fill='yellow') x2=10000 w.itemconfig(t4,text=x2) def fun72(): linija = w.create_line(180, 60, 180, 140, width='15',fill='green') x2=100000 w.itemconfig(t4,text=x2) def fun82(): linija = w.create_line(180, 60, 180, 140, width='15',fill='blue') x2=1000000 w.itemconfig(t4,text=x2) def fun92(): linija = w.create_line(180, 60, 180, 140, width='15',fill='purple') x2=10000000 w.itemconfig(t4,text=x2) def fun102(): linija = w.create_line(180, 60, 180, 140, width='15',fill='#EAC117') x2=0.01 w.itemconfig(t4,text=x2) def fun112(): linija = w.create_line(180, 60, 180, 140, width='15',fill='#C0C0C0') x2=0.1 w.itemconfig(t4,text=x2) #4linija def fun23(): linija = w.create_line(260, 60, 260, 140, width='15',fill='brown') x3=1 w.itemconfig(t5,text=x3) def fun33(): linija = w.create_line(260, 60, 260, 140, width='15',fill='red') x3=2 w.itemconfig(t5,text=x3) def fun43(): linija = w.create_line(260, 60, 260, 140, width='15',fill='#EAC117') x3=5 w.itemconfig(t5,text=x3) def fun53(): linija = w.create_line(260, 60, 260, 140, width='15',fill='#C0C0C0') x3=10 w.itemconfig(t5,text=x3) t1 = w.create_text(1, 200, text="first color: ",font="Courier 10 bold ", fill ="black",anchor='w') b1=tk.Button(root, text="black ", command=fun2,width=8) b1.pack() b1.place(x=5 ,y=220) b2=tk.Button(root, text="brown", command=fun3,width=8) b2.pack() b2.place(x=5 ,y=250) b3=tk.Button(root, text="red ", command=fun4,width=8) b3.pack() b3.place(x=5 ,y=280) b4=tk.Button(root, text="orange", command=fun5,width=8) b4.pack() b4.place(x=5 ,y=310) b5=tk.Button(root, text="yellow", command=fun6,width=8) b5.pack() b5.place(x=5 ,y=340) b6=tk.Button(root, text="green", command=fun7,width=8) b6.pack() b6.place(x=5 ,y=370) b7=tk.Button(root, text="blue", command=fun8,width=8) b7.pack() b7.place(x=5 ,y=400) b8=tk.Button(root, text="purple", command=fun9,width=8) b8.pack() b8.place(x=5 ,y=430) b9=tk.Button(root, text="gray", command=fun10,width=8) b9.pack() b9.place(x=5 ,y=460) b10=tk.Button(root, text="white", command=fun11,width=8) b10.pack() b10.place(x=5 ,y=490) t11 = w.create_text(150, 200, text="second color: ",font="Courier 10 bold ", fill ="black",anchor='w') b11=tk.Button(root, text="black ", command=fun21,width=8) b11.pack() b11.place(x=150 ,y=220) b21=tk.Button(root, text="brown", command=fun31,width=8) b21.pack() b21.place(x=150 ,y=250) b31=tk.Button(root, text="red ", command=fun41,width=8) b31.pack() b31.place(x=150 ,y=280) b41=tk.Button(root, text="orange", command=fun51,width=8) b41.pack() b41.place(x=150 ,y=310) b51=tk.Button(root, text="yellow", command=fun61,width=8) b51.pack() b51.place(x=150 ,y=340) b61=tk.Button(root, text="green", command=fun71,width=8) b61.pack() b61.place(x=150 ,y=370) b71=tk.Button(root, text="blue", command=fun81,width=8) b71.pack() b71.place(x=150 ,y=400) b81=tk.Button(root, text="purple", command=fun91,width=8) b81.pack() b81.place(x=150 ,y=430) b91=tk.Button(root, text="gray", command=fun101,width=8) b91.pack() b91.place(x=150 ,y=460) b101=tk.Button(root, text="white", command=fun111,width=8) b101.pack() b101.place(x=150 ,y=490) t1 = w.create_text(300, 200, text="third color: ",font="Courier 10 bold ", fill ="black",anchor='w') b1=tk.Button(root, text="black ", command=fun22,width=8) b1.pack() b1.place(x=300 ,y=220) b2=tk.Button(root, text="brown", command=fun32,width=8) b2.pack() b2.place(x=300 ,y=250) b3=tk.Button(root, text="red ", command=fun42,width=8) b3.pack() b3.place(x=300 ,y=280) b4=tk.Button(root, text="orange", command=fun52,width=8) b4.pack() b4.place(x=300 ,y=310) b5=tk.Button(root, text="yellow", command=fun62,width=8) b5.pack() b5.place(x=300 ,y=340) b6=tk.Button(root, text="green", command=fun72,width=8) b6.pack() b6.place(x=300 ,y=370) b7=tk.Button(root, text="blue", command=fun82,width=8) b7.pack() b7.place(x=300 ,y=400) b8=tk.Button(root, text="purple", command=fun92,width=8) b8.pack() b8.place(x=300 ,y=430) b9=tk.Button(root, text="gold", command=fun102,width=8) b9.pack() b9.place(x=300 ,y=460) b10=tk.Button(root, text="silver", command=fun112,width=8) b10.pack() b10.place(x=300 ,y=490) t1 = w.create_text(450, 200, text="fourth color: ",font="Courier 10 bold ", fill ="black",anchor='w') b2=tk.Button(root, text="brown", command=fun23,width=8) b2.pack() b2.place(x=450 ,y=220) b3=tk.Button(root, text="red ", command=fun33,width=8) b3.pack() b3.place(x=450 ,y=250) b11=tk.Button(root, text="gold", command=fun43,width=8) b11.pack() b11.place(x=450 ,y=280) b12=tk.Button(root, text="silver", command=fun53,width=8) b12.pack() b12.place(x=450 ,y=310) t2=w.create_text(500, 20, text=" 44 ") t3=w.create_text(500, 50, text=" 55 ") t4=w.create_text(500, 80, text=" 66 ") t5=w.create_text(500, 110, text=" 77 ") t6=w.create_text(500, 140, text=" 88 ") def funf(): z=(x+x1)*x2 w.itemconfig(t6,text=z) bfin=tk.Button(root, text="CALCULATE", command=funf,font="24") bfin.pack() bfin.place(x=400 ,y=500) w.mainloop() The main problem is that I can`t read variables x, x1, x2 on button "CALCULATE" press. Thank you for support! |
Hi,
Thus spoketh "ed(LV)" <[hidden email]> unto us on Sat, 3 Mar 2012 23:38:17 -0800 (PST): > *Hello! > Im beginner at Tkinter and this is my 1st project. > I want make programm that calculate resistor value from colors, but i > can`t get variables out of functions. > Here you can see all code:* > (...) > *The main problem is that I can`t read variables x, x1, x2 on button > "CALCULATE" press. > Thank you for support!* when I run you code I get each time I press The "Calculate" button the error message: Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__ return self.func(*args) File "test7.py", line 326, in funf z=(x+x1)*x2 NameError: global name 'x' is not defined which shows clearly what happens: the callback def funf(): z=(x+x1)*x2 w.itemconfig(t6,text=z) tries to use a variable "x" which is not defined in the function's namespace, which covers basically everything that is defined *globally* plus anything defined within the function's *local* namespace, which means in the body of the function itself. I set up a minimal example to illustrate the difference between a local and a global variable, I hope you see the point: ########### file test.py ################### from Tkinter import * root = Tk() x = 1 def func1(): x = 0 print 'value of x:', x def func2(): global x print 'value of x:', x def func3(): global x x += 2 print 'value of x:', x Button(text='Func 1', command=func1).pack() Button(text='Func 2', command=func2).pack() Button(text='Func 3', command=func3).pack() root.mainloop() ########################################### So possible solutions for your problem might be: * declare global variables in your functions ( I haven't properly investigated all of your code, but I guess you would probably have to declare globals in all of your button callbacks, which is most often considered to be bad practice, although it will work without problems if it is done carefully), or * use Tkinter variables , as it seems was one of your first tries. Then you can simply call something like var1.set(1) from within the function body. If you want to learn more about namespaces in Python and how they work, a good starting point is: http://www.freenetpages.co.uk/hp/alan.gauld/tutname.htm I hope this helps Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. One of the advantages of being a captain is being able to ask for advice without necessarily having to take it. -- Kirk, "Dagger of the Mind", stardate 2715.2 _______________________________________________ Tkinter-discuss mailing list [hidden email] http://mail.python.org/mailman/listinfo/tkinter-discuss |
Free forum by Nabble | Edit this page |