Search code examples
pythonuser-interfacefiremonkey

How to change Label font size in Python FMX GUI app


I'm using the DelphiFMX GUI library for Python and trying to change the font size on a Label component, but it's not working.

I have the following code to create the Form and the Label on my Form:

from delphifmx import *

class HelloForm(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.TextSettings.Font.Size = 50
        self.myLabel.TextSettings.HorzAlign = "Center"

My output Form, then looks like this: Python GUI Hello World

My "Hello World!" Label should be much bigger than what it's showing. I'm setting the font size with this piece of code:

self.myLabel.TextSettings.Font.Size = 50

Solution

  • Ah. After playing with the code for a bit, I realized that I needed to add the following line of code in order to make sure the Label isn't being styled by the Style Manager:

    self.myLabel.StyledSettings = ""
    

    If you don't clear the StyledSettings, then it will use default styling on the Label component. After adding that line of code, my Label now works and shows correctly: Python GUI Hello World

    So my full code now looks like this and works:

    from delphifmx import *
    
    class HelloForm(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 = 50
            self.myLabel.TextSettings.HorzAlign = "Center"
    
    def main():
        Application.Initialize()
        Application.Title = "Hello World"
        Application.MainForm = HelloForm(Application)
        Application.MainForm.Show()
        Application.Run()
        Application.MainForm.Destroy()
    
    main()