Search code examples
pythonandroidcanvasbeeware

Beeware Toga Canvas: working on windows but not on android


I am trying to use Toga Canvas for my Beeware app. According to Toga docs, Canvas should be fully functional on android https://toga.readthedocs.io/en/latest/reference/api/widgets/canvas.html, To check it I use the simple script attached below, where upon pressing the draw button, a red rectangle sould be drawn. The script works well with windows, but with android nothing happens upon pressing the button. I tried on both the Beeware emulator and on my Galaxy 23 and the result is the same. Any suggestion or hint on how to make it work will be much appreciated.

import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW

class HelloWorld(toga.App):
    def startup(self):
        global canvas
        main_box = toga.Box(style=Pack(direction=COLUMN))

        test_button = toga.Button("draw",on_press=draw_rect,style=Pack(padding=5))
        canvas = toga.Canvas(style=Pack(flex=1))
        main_box.add(canvas)
        main_box.add(test_button)

        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main_box
        self.main_window.show()

def draw_rect(widget):
    with canvas.fill(color="red") as Rect:
        Rect.rect(x=50,y=50,width=25,height=15)

def main():
    return HelloWorld()

Solution

  • The "latest" readthedocs page is for the development version, which hasn't been released yet. To see the documentation of the last released version, change "latest" to "stable".

    To use the development version, which has many improvements to the Canvas, edit your pyproject.toml file to replace toga-android with the following:

        "git+https://github.com/beeware/toga#subdirectory=core",
        "git+https://github.com/beeware/toga#subdirectory=android",