2

In my app I need to know how big the canvas is in pixels.

Instead calling canvas.size returns [100,100] no matter how many pixels the canvas is wide.

Can you please tell me a way to get how many pixels the canvas is wide and high?

Caridorc
  • 6,222
  • 2
  • 31
  • 46

3 Answers3

4

There is no position nor size in a Canvas. Canvas act just as a container for graphics instructions, like Fbo that draw within a Texture, so it have a size.

In Kivy, Canvas.size doesn't exists, but i guess you called your widget a canvas. By default, a Widget size is 100, 100. If you put it into a layout, the size will be changed, when the layout will known its own size. Mean, you need to listen to the changes of the Widget.size, or use a size you known, like Window.size.

tito
  • 12,990
  • 1
  • 55
  • 75
3

I'm gonna guess that you actually need the window size in order to know what your image boundaries are. In this case, you can use:

from kivy.core.window import Window
size = Window.size

And you can of course use the source image of your canvas with PIL or scipy etc. to get its resolution (which the canvas will match).

runDOSrun
  • 10,359
  • 7
  • 47
  • 57
0

It seems that the canvas has a "size" of (100,100) whatever you do. The size you are looking for must be the widget size e.g. If you have a FloatLayout and want to draw on it a rect that fills it try:

f = FloatLayout
Rectangle(pos_hint={'x': 0, 'y': 0}, size=f.size)

and not

Rectangle(pos_hint={'x': 0, 'y': 0}, size=f.canvas.size)
rlandster
  • 7,294
  • 14
  • 58
  • 96