Search code examples
pythonuser-interfacefiremonkey

How to add a border to a label in a Python FMX GUI App?


I've made a simple Hello World! app in the DelphiFMX GUI Library for Python. "Hello World!" is shown on a Label on the Form as can be seen below with my code and screenshot:

from delphifmx import *

class frmMain(Form):
    def __init__(self, owner):
        self.Caption = 'Hello World'
        self.Width = 1000
        self.Height = 500
        self.Position = "ScreenCenter"

        self.myLabel = Label(self)
        self.myLabel.Parent = self
        self.myLabel.Text = "Hello World!"
        self.myLabel.Align = "Client"
        self.myLabel.StyledSettings = ""
        self.myLabel.TextSettings.Font.Size = 85
        self.myLabel.TextSettings.HorzAlign = "Center"


Application.Initialize()
Application.Title = "My Application"
Application.MainForm = frmMain(Application)
Application.MainForm.Show()
Application.Run()
Application.MainForm.Destroy()

Python GUI Hello World App

Is there a way to add a border around the Label? How would one do this?

I tried doing things like the following code, but it doesn't work:

self.myLabel.Stroke.Kind = "Solid"
self.myLabel.Stroke.Color = "Black"
self.myLabel.Stroke.Thickness = 1

Is it possible to add a border around a Label?, if yes, then how?


Solution

  • The Label component doesn't have a Border property by default, but there is a way. The easiest way to give a Border to a Label isn't to actually give a border to it, but rather to make a Rectangle and then put the Label into the Rectangle:

    self.myRectangle = Rectangle(self)
    self.myRectangle.Parent = self
    self.myRectangle.Align = "Center"
    self.myRectangle.Width = 525
    self.myRectangle.Height = 150
    self.myRectangle.Fill.Color = "$0"
    
    self.myLabel = Label(self)
    self.myLabel.Parent = self.myRectangle
    self.myLabel.Text = "Hello World!"
    self.myLabel.Align = "Client"
    self.myLabel.StyledSettings = ""
    self.myLabel.TextSettings.Font.Size = 85
    self.myLabel.TextSettings.HorzAlign = "Center"
    

    Python Hello World GUI App

    The Rectangle component has a border by default and you can give it a transparent background by doing self.myRectangle.Fill.Color = "$0". You can also adjust the Color and Thickness of the Border with the following code:

    self.myRectangle.Stroke.Color = "$FFFF0000"
    self.myRectangle.Stroke.Thickness = 15
    

    Python GUI App with Hello World Label