如何在 Python FMX GUI 应用程序中更改标签字体大小

问题描述 投票:0回答:1

我正在使用 Python 的 DelphiFMX GUI 库 并尝试更改

Label
组件上的字体大小,但它不起作用。

我有以下代码在我的表格上创建

Form
Label

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"

我的输出表单,然后是这样的:

我的“你好世界!”标签应该比它显示的大得多。我正在用这段代码设置字体大小:

self.myLabel.TextSettings.Font.Size = 50
python firemonkey
1个回答
0
投票

啊。玩了一会儿代码后,我意识到我需要添加以下代码行以确保

Label
没有被样式管理器设置样式:

self.myLabel.StyledSettings = ""

如果您不清除

StyledSettings
,那么它将在Label 组件上使用默认样式。添加该行代码后,我的 Label 现在可以正常工作并正确显示:

所以我的完整代码现在看起来像这样并且有效:

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()
© www.soinside.com 2019 - 2024. All rights reserved.