BoxSizer 问题:未扩展以适应 StaticText 的内容
我正在创建一个带有多个子 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
知道发生了什么吗?
编辑:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从未见过有人创建这样的 sizer。通常您会这样做:
然后添加每个元素。我怀疑既然你以一种奇怪的方式做这件事,你就会得到奇怪的结果。不要为此使用 StaticText 的 sizer 方法。它们不是容器对象。相反,请执行以下操作:
然后像之前一样添加其他小部件。并且告诉 sizer 设置自身也无法正常工作:“block.SetSizerAndFit(block.sizer)”。您需要调用父窗口小部件的 SetSizer() 方法,该方法可能是此处的侧边栏:
或者您可以按照此处的方式添加它:
编辑
这是一个简单的代码示例:
我个人不这样做不太喜欢 Fit() ,但在这种情况下,它对我有用。
I've never seen anyone create a sizer like that. Normally you do this:
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:
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:
or maybe you can just add it the way you had it here:
EDIT
Here's a simple code example:
I personally don't like Fit() much, but in this case, it works for me.