如何在WxPython中使标题栏的高度适应新的标题字体大小的增加?

tzxcd3kk  于 2022-12-05  发布在  Python
关注(0)|答案(1)|浏览(316)

I've increased the custon AddPrivateFont pointsize to

self.label_font.SetPointSize(27)

of the Title bar of the sample_one.py script from this shared project:
https://wiki.wxpython.org/How%20to%20add%20a%20menu%20bar%20in%20the%20title%20bar%20%28Phoenix%29
From the script of my previous question here:
https://web.archive.org/web/20221202192613/https://paste.c-net.org/HondoPrairie
AddPrivateFont to App Title / Title bar in WxPython?
My problem is I can't figure out how to make the Title bar's height larger so the Title text displays its top part correctly. Currently the top of the Title text is truncated.
I tried adjusting the height and textHeight values from this statement:

textWidth, textHeight = gcdc.GetTextExtent(self.label)
        tposx, tposy = ((width / 2) - (textWidth / 2), (height / 1) - (textHeight / 1))

from previous ones (in the sample_one.py script):

textWidth, textHeight = gcdc.GetTextExtent(self.label)
        tposx, tposy = ((width / 2) - (textWidth / 2), (height / 3) - (textHeight / 3))

Because it truncated the bottom (now the bottom shows up correctly but not the top of the Title text).
There is also this method I'm not sure how to handle:

def DoGetBestSize(self):
        """
        ...
        """

        dc = wx.ClientDC(self)
        dc.SetFont(self.GetFont())

        textWidth, textHeight = dc.GetTextExtent(self.label)
        spacing = 10
        totalWidth = textWidth + (spacing)
        totalHeight = textHeight + (spacing)

        best = wx.Size(totalWidth, totalHeight)
        self.CacheBestSize(best)

        return best

I tried tweaking it and printing results but to no avail.
Here's a preview of the Truncated Title text:

What would be the correct approach to finding out what controls the height of the title bar object to fix the truncated title text?

dxxyhpgq

dxxyhpgq1#

感谢@Rolf of萨克森的headsup,我想明白了!
它采取了以下3个步骤:

第一步:
顶部标题文本显示自:

class MyTitleBarPnl(wx.Panel):
    def CreateCtrls(self):
        self.titleBar.SetSize((w, 54))

    def OnResize(self, event):
        self.titleBar.SetSize((w, 54))

第二步:
标题文本下方的垂直间距(不显示文本):

class MyFrame(wx.Frame):
    def CreateCtrls(self):
        self.titleBarPnl = MyTitleBarPnl(self, -1, (w, 54))

    def OnResize(self, event):
        self.titleBarPnl.SetSize((w, 24))

第三步:
带文本显示的标题文本下方的垂直间距:

class MyFrame(wx.Frame):
    def CreateCtrls(self):
        self.titleBarPnl = MyTitleBarPnl(self, -1, (w, 54))

    def OnResize(self, event):
        self.titleBarPnl.SetSize((w, 54))

编辑:
第四步:
状态栏显示:

class MyFrame(wx.Frame):
    def CreateCtrls(self):
        self.titleBarPnl = MyTitleBarPnl(self, -1, (w, 54))

    def OnResize(self, event):
        self.titleBarPnl.SetSize((w, 54))
        self.mainPnl.SetSize((w, h - 55))  # 25

相关问题