首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >面板之间的元素在sizer中不对齐。

面板之间的元素在sizer中不对齐。
EN

Stack Overflow用户
提问于 2014-07-22 15:45:16
回答 1查看 63关注 0票数 0

我有两个面板,我都是添加到一个单一的sizer在最高水平,但在这些面板内的元素是不对齐的彼此。

这里有一个简单的例子来说明我正在努力实现的目标。

代码语言:javascript
复制
import wx

class MyPanel(wx.Panel):
    def __init__(self, parent):
        super(MyPanel, self).__init__(parent=parent)
        mygridsizer = wx.GridBagSizer()    

        sizer = wx.BoxSizer(orient=wx.HORIZONTAL)
        sizer.Add(wx.StaticText(self,label="Hello world"))
        sizer.Add(wx.Button(self, label="hello"))
        mygridsizer.Add(sizer, pos=(0,0))
        mygridsizer.Add(wx.ComboBox(self), pos=(0,1))
        self.SetSizer(mygridsizer)

class MyPanel2(wx.Panel):
    def __init__(self, parent):
        super(MyPanel2, self).__init__(parent=parent)
        sizer = wx.BoxSizer(orient=wx.HORIZONTAL)
        sizer.Add(wx.Button(self, label="non-aligned button"))
        self.SetSizer(sizer)

class MainFrame(wx.Frame):
    def __init__(self, parent):
        super(MainFrame, self).__init__(None)
        sizer = wx.GridSizer(3, 1)
        panel1 = MyPanel(parent=self)
        panel2 = MyPanel2(parent=self)
        sizer.Add(panel1)
        sizer.Add(panel2)
        self.SetSizer(sizer)

if __name__ == '__main__':
    app = wx.App()
    frame = MainFrame(None)
    frame.Show()
    app.MainLoop()

有了上面的例子,我能做些什么来对齐两个面板的按钮?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-14 22:13:11

问题是,sizer是对齐面板,如果你想让按钮对齐,你应该让他们成为相同的大小(而不是两个面板的一部分,他们自己的尺寸)。您还可以为快速攻击(实质上添加与文本大小相同的间隔)执行类似的操作:

代码语言:javascript
复制
import wx

class MyPanel(wx.Panel):
    def __init__(self, parent):
        super(MyPanel, self).__init__(parent=parent)
        mygridsizer = wx.GridBagSizer()    

        sizer = wx.BoxSizer(orient=wx.HORIZONTAL)
        sizer.Add(wx.StaticText(self,label="Hello world"))
        sizer.Add(wx.Button(self, label="hello"))
        mygridsizer.Add(sizer, pos=(0,0))
        mygridsizer.Add(wx.ComboBox(self), pos=(0,1))
        self.SetSizer(mygridsizer)

class MyPanel2(wx.Panel):
    def __init__(self, parent):
        super(MyPanel2, self).__init__(parent=parent)
        sizer = wx.BoxSizer(orient=wx.HORIZONTAL)
        t = wx.StaticText(self,label="Hello world")
        t.Hide()
        t.GetSize()
        sizer.Add(t.GetSize())
        sizer.Add(wx.Button(self, label="non-aligned button"))
        self.SetSizer(sizer)

class MainFrame(wx.Frame):
    def __init__(self, parent):
        super(MainFrame, self).__init__(None)
        sizer = wx.GridSizer(3, 1)
        panel1 = MyPanel(parent=self)
        panel2 = MyPanel2(parent=self)
        sizer.Add(panel1)
        sizer.Add(panel2)
        self.SetSizer(sizer)

if __name__ == '__main__':
    app = wx.App()
    frame = MainFrame(None)
    frame.Show()
    app.MainLoop()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24891936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档