BoxSizer 问题:未扩展以适应 StaticText 的内容

发布于 2024-12-19 23:27:55 字数 1742 浏览 1 评论 0原文

我正在创建一个带有多个子 StaticText 元素的 StaticText 元素,这些元素的大小不太正确。我确定这是 BoxSizer 问题,但也许不是?这是正在发生的事情的屏幕截图: http://cloud.smallparade.com/CMxC

应该是

Wilco
One Sunday Morning (Song
For Jane Smiley's Boyfriend)
The Whole Love (Deluxe Edition)

:您可能会推断出,第二行和第三行(Album)是一个 StaticText 元素,它正在换行,但不会扩展以适合其内容。

以下是创建此元素的代码:

def PlayLabel(self, artist, track, album):
    header_font = wx.Font(26, wx.SWISS, wx.NORMAL, wx.BOLD, False, "Helvetica")
    album_font = wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD, False, "Helvetica")

    # label container
    block = wx.StaticText(self.Sidebar, -1, '')

    block.sizer = wx.BoxSizer(wx.VERTICAL)
    block.SetBackgroundColour('WHITE')

    label_artist = wx.StaticText(block, -1, artist)
    label_artist.SetForegroundColour('BLACK')
    label_artist.SetFont(header_font)

    label_track = wx.StaticText(block, -1, track)
    label_track.SetForegroundColour('BLACK')
    label_track.SetFont(header_font)

    label_album = wx.StaticText(block, -1, album)
    label_album.SetForegroundColour('BLACK')
    label_album.SetFont(album_font)

    block.sizer.AddMany([
        (label_artist, 1, wx.EXPAND|wx.TOP|wx.LEFT|wx.RIGHT, 5),
        (label_track, 1, wx.EXPAND|wx.LEFT|wx.RIGHT, 5),
        (label_album, 1, wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, 5)
    ])
    block.SetSizerAndFit(block.sizer)

    self.Sidebar.sizer.Add(block, 0, wx.EXPAND|wx.ALIGN_LEFT|wx.BOTTOM, 5)
    self.Sidebar.sizer.Layout()
    self.Layout()
    return block

知道发生了什么吗?

编辑:

完整代码:http://pastie.org/2976327

I'm creating a StaticText element with multiple child StaticText elements that aren't quite sizing correctly. I'm sure it's a BoxSizer issue, but maybe not? Here's a screenshot of what's happening: http://cloud.smallparade.com/CMxC

That should read:

Wilco
One Sunday Morning (Song
For Jane Smiley's Boyfriend)
The Whole Love (Deluxe Edition)

As you can likely deduce, the second and third lines (Album) are one StaticText element that's wrapping but not expanding to fit its contents.

Here is the code that creates this element:

def PlayLabel(self, artist, track, album):
    header_font = wx.Font(26, wx.SWISS, wx.NORMAL, wx.BOLD, False, "Helvetica")
    album_font = wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD, False, "Helvetica")

    # label container
    block = wx.StaticText(self.Sidebar, -1, '')

    block.sizer = wx.BoxSizer(wx.VERTICAL)
    block.SetBackgroundColour('WHITE')

    label_artist = wx.StaticText(block, -1, artist)
    label_artist.SetForegroundColour('BLACK')
    label_artist.SetFont(header_font)

    label_track = wx.StaticText(block, -1, track)
    label_track.SetForegroundColour('BLACK')
    label_track.SetFont(header_font)

    label_album = wx.StaticText(block, -1, album)
    label_album.SetForegroundColour('BLACK')
    label_album.SetFont(album_font)

    block.sizer.AddMany([
        (label_artist, 1, wx.EXPAND|wx.TOP|wx.LEFT|wx.RIGHT, 5),
        (label_track, 1, wx.EXPAND|wx.LEFT|wx.RIGHT, 5),
        (label_album, 1, wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, 5)
    ])
    block.SetSizerAndFit(block.sizer)

    self.Sidebar.sizer.Add(block, 0, wx.EXPAND|wx.ALIGN_LEFT|wx.BOTTOM, 5)
    self.Sidebar.sizer.Layout()
    self.Layout()
    return block

Any idea what's going on?

EDIT:

Complete code: http://pastie.org/2976327

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揪着可爱 2024-12-26 23:27:55

我从未见过有人创建这样的 sizer。通常您会这样做:

mySizer = wx.BoxSizer(wx.VERTICAL)

然后添加每个元素。我怀疑既然你以一种奇怪的方式做这件事,你就会得到奇怪的结果。不要为此使用 StaticText 的 sizer 方法。它们不是容器对象。相反,请执行以下操作:

block = wx.BoxSizer(wx.VERTICAL)

然后像之前一样添加其他小部件。并且告诉 sizer 设置自身也无法正常工作:“block.SetSizerAndFit(block.sizer)”。您需要调用父窗口小部件的 SetSizer() 方法,该方法可能是此处的侧边栏:

self.Sidebar.SetSizer(block)

或者您可以按照此处的方式添加它:

self.Sidebar.sizer.Add(block, 0, wx.EXPAND|wx.ALIGN_LEFT|wx.BOTTOM, 5)

编辑

这是一个简单的代码示例:

import wx

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Test")

        self.panel = wx.Panel(self)
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        samples = [("Red", "Breathe Into Me", "End of Silence"),
                   ("eleventyseven", "Lonely", "Sugarfist"),
                   ("Lecrae", "Go Hard", "Rebel")]
        for sample in samples:
            artist, track, album = sample
            self.PlayLabel(artist, track, album)

        self.panel.SetSizer(self.mainSizer)
        panelSizer = wx.BoxSizer()
        panelSizer.Add(self.panel, 1, wx.EXPAND)
        self.SetSizer(panelSizer)
        self.Fit()


    def PlayLabel(self, artist, track, album):
        header_font = wx.Font(26, wx.SWISS, wx.NORMAL, wx.BOLD, False, "Helvetica")
        album_font = wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD, False, "Helvetica")

        block = self.panel

        # sizer
        sizer = self.mainSizer

        # artist
        label_artist = wx.StaticText(block, -1, artist)
        label_artist.SetForegroundColour('BLACK')
        label_artist.SetFont(header_font)

        # track
        label_track = wx.StaticText(block, -1, track)
        label_track.SetForegroundColour('BLACK')
        label_track.SetFont(header_font)

        # album
        label_album = wx.StaticText(block, -1, album)
        label_album.SetForegroundColour('BLACK')
        label_album.SetFont(album_font)

        sizer.AddMany([
                (label_artist, 1, wx.EXPAND|wx.TOP|wx.LEFT|wx.RIGHT, 5),
                (label_track, 1, wx.EXPAND|wx.LEFT|wx.RIGHT, 5),
                (label_album, 1, wx.EXPAND|wx.LEFT|wx.RIGHT, 5)
        ])

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

我个人不这样做不太喜欢 Fit() ,但在这种情况下,它对我有用。

I've never seen anyone create a sizer like that. Normally you do this:

mySizer = wx.BoxSizer(wx.VERTICAL)

Then you add each element. I suspect that since you're doing it in a weird way, you're getting weird results. Don't use the StaticText's sizer method for this. They are not container objects. Instead, do this:

block = wx.BoxSizer(wx.VERTICAL)

And then add the other widgets as you did before. And telling the sizer to set itself won't work right either: "block.SetSizerAndFit(block.sizer)". You need to call the parent widget's SetSizer() method, which is probably the Sidebar here:

self.Sidebar.SetSizer(block)

or maybe you can just add it the way you had it here:

self.Sidebar.sizer.Add(block, 0, wx.EXPAND|wx.ALIGN_LEFT|wx.BOTTOM, 5)

EDIT

Here's a simple code example:

import wx

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Test")

        self.panel = wx.Panel(self)
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        samples = [("Red", "Breathe Into Me", "End of Silence"),
                   ("eleventyseven", "Lonely", "Sugarfist"),
                   ("Lecrae", "Go Hard", "Rebel")]
        for sample in samples:
            artist, track, album = sample
            self.PlayLabel(artist, track, album)

        self.panel.SetSizer(self.mainSizer)
        panelSizer = wx.BoxSizer()
        panelSizer.Add(self.panel, 1, wx.EXPAND)
        self.SetSizer(panelSizer)
        self.Fit()


    def PlayLabel(self, artist, track, album):
        header_font = wx.Font(26, wx.SWISS, wx.NORMAL, wx.BOLD, False, "Helvetica")
        album_font = wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD, False, "Helvetica")

        block = self.panel

        # sizer
        sizer = self.mainSizer

        # artist
        label_artist = wx.StaticText(block, -1, artist)
        label_artist.SetForegroundColour('BLACK')
        label_artist.SetFont(header_font)

        # track
        label_track = wx.StaticText(block, -1, track)
        label_track.SetForegroundColour('BLACK')
        label_track.SetFont(header_font)

        # album
        label_album = wx.StaticText(block, -1, album)
        label_album.SetForegroundColour('BLACK')
        label_album.SetFont(album_font)

        sizer.AddMany([
                (label_artist, 1, wx.EXPAND|wx.TOP|wx.LEFT|wx.RIGHT, 5),
                (label_track, 1, wx.EXPAND|wx.LEFT|wx.RIGHT, 5),
                (label_album, 1, wx.EXPAND|wx.LEFT|wx.RIGHT, 5)
        ])

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

I personally don't like Fit() much, but in this case, it works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文