#this will explain the scope of the variable
#we can use locals() to know the variable in that scopre,this can be used only inside functions
import tkinter
def parabola(x):
y = x * x/100
return y
def draw_axes(canvas):
#the canvas variable here is different from the canvas outside
#of this function
# canvas.update
#here the x and y origin are not fetching properly so hardcoded the values
# x_origin = canvas.winfo_width ()/2
# y_origin = canvas.winfo_height()/2
# x_origin = canvas.winfo_width ()
# y_origin = canvas.winfo_height()
canvas.configure(scrollregion=(-200,-200,200,200))
canvas.create_line(-200,0,200,0, fill="blue")
canvas.create_line(0,200,0,-200, fill="blue")
#this will give the object location(address),we can differentiate object of same names
#using this id()
print(id(canvas))
print(locals())
def plot(canvas,x,y):
canvas.create_line(x,y,x+1,y+1,fill="red")
mainwindow = tkinter.Tk()
mainwindow.title("Parabola")
mainwindow.geometry("640x480")
canvas = tkinter.Canvas(mainwindow,width=320,height= 480)
canvas.grid(row=0,column=0)
#we have created two canvas to show how the scope works
canvas2 = tkinter.Canvas(mainwindow,width=320,height= 480)
canvas2.grid(row=0,column=1)
print(id(canvas))
print(id(canvas2))
draw_axes(canvas)
draw_axes(canvas2)
for x in range(-100,100):
y = parabola(x)
plot(canvas,x,-y)
mainwindow.mainloop()
Friday, 6 April 2018
Python function scope - Day 20(after 20 days)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment