When referencing a variable of a class: it does not save data

1

When I create my class, I create a variable called

self.canvas_height= self.canvas.winfo_height()

in which I store the result number of a method I invoke. So much the better but after that same class I create a function dibujar where at some point I try to call variable self.canvas_height and it does not work ...

BUT I was testing with the second variable width and instead of invoking the data using the variable, I said Ok let's try calling the function directly, in this case it worked.

My question is, why does not it work in the Height case when I call the variable?

from Tkinter import *
import time
import random

class pelota:
        def __init__(self,canvas,color):

            self.canvas_height= self.canvas.winfo_height()
            self.canvas_width = self.canvas.winfo_width()
        def dibujar(self):

            if pos[2]>=self.canvas.winfo_width():


            if pos[3]>=self.canvas_height:
    
asked by Shiki 17.02.2017 в 17:33
source

1 answer

1

The problem is in the init method.

First: -You are sending self (instance of the class), canvas (I assume it is a canvas object), color (do not use it?).

Therefore when declaring:

self.canvas_height = self.canvas.winfo_height() # Esta mal porque no existe self.canvas
#No existe niguna variable self.canvas, no ha sido declarada aún.

Solution: Declare the variable:

self.canvas = canvas # canvas = objeto canvas que envias.

Or use the canvas object you send directly.

self.canvas_height = canvas.winfo_height()
    
answered by 22.02.2017 в 21:25