Search code examples
pythonbeewaretoga

How to get the dimensions of a toga.Canvas() in Python?


In a Python BeeWare project, I want to use toga.Canvas to draw some horizontal rectangles, but I don't know from where to get the Canvas width. I can't find any documentation for the toga.Canvas() dimensions on the internet...

  def redraw_canvas(self):
    x = 4; y = 4;  
    for i in range(7):  
      with self.canvas.context.Fill(color=self.clRowBkg) as fill:
        fill.rect(x, y, 100, self.row_height)
      y += self.row_height + 4

Solution

  • There doesn't appear to be any way to get this information from the public API, but there are some non-public properties used in the Toga Canvas example:

    • canvas.layout.content_width
    • canvas.layout.content_height

    These should work for any Toga widget, not just Canvas.