Why does icon not show up when button generated from python code?

This is probably happening because you are using the base Button instead of the M3 Beta button.

The short answer is: just add the following to the top of your form below other imports but above the class definition:

from m3.components import Button

The long answer: the M3 Beta version of anvil apps replace a lot of basic elements in the designer, one of them being the button. However, in code, when you create a Button, you are creating the native anvil Button, not the M3. So you need to manually import to the context to replace the original in that context.

You can also do this globally in you startup module (if you have one) by doing:

# not tested!
from m3.components import Button as NewButton
import anvil
anvil.Button = NewButton

This makes that the default button will be M3.Button

Also, the icon doesn’t work because the old button uses a different type of icon: from fontawesome. They start with 'fa:...' and work a little different, so that’s why it didn’t work.

3 Likes