Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wxpython:将文本居中放置在sizer内的面板内_Python_Wxpython_Panel_Center_Sizer - Fatal编程技术网

wxpython:将文本居中放置在sizer内的面板内

wxpython:将文本居中放置在sizer内的面板内,python,wxpython,panel,center,sizer,Python,Wxpython,Panel,Center,Sizer,在我看来,下面的代码应该在窗口的正中央显示文本;也就是说,在内部面板的中心。但事实并非如此,我想知道为什么不能。如果你运行代码,你会看到一个白色的面板在帧的中间,150 px到150 px。我不希望这个区域在大小上发生变化,但是当我开始添加一些文本时(在代码段中间不注释 txt < /COD>变量),面板总是缩小以适合文本。即使指定StaticText的大小以匹配面板也不是一个解决方案,因为此时文本不会居中对齐 import wx class MyFrame(wx.Frame): de

在我看来,下面的代码应该在窗口的正中央显示文本;也就是说,在内部面板的中心。但事实并非如此,我想知道为什么不能。如果你运行代码,你会看到一个白色的面板在帧的中间,150 px到150 px。我不希望这个区域在大小上发生变化,但是当我开始添加一些文本时(在代码段中间不注释<代码> txt < /COD>变量),面板总是缩小以适合文本。即使指定
StaticText
的大小以匹配面板也不是一个解决方案,因为此时文本不会居中对齐

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)




        self.rootPanel = wx.Panel(self)

        innerPanel = wx.Panel(self.rootPanel,-1, size=(150,150), style=wx.ALIGN_CENTER)
        innerPanel.SetBackgroundColour('WHITE')
        hbox = wx.BoxSizer(wx.HORIZONTAL) 
        vbox = wx.BoxSizer(wx.VERTICAL)

        # I want this line visible in the CENTRE of the inner panel
        #txt = wx.StaticText(innerPanel, id=-1, label="TEXT HERE",style=wx.ALIGN_CENTER, name="")

        hbox.Add(innerPanel, 0, wx.ALL|wx.ALIGN_CENTER)
        vbox.Add(hbox, 1, wx.ALL|wx.ALIGN_CENTER, 5)

        self.rootPanel.SetSizer(vbox)
        vbox.Fit(self)

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'wxBoxSizer.py')
        frame.Show(True)
        frame.Center()
        return True

app = MyApp(0)
app.MainLoop()

您只需要添加几个垫片就可以了

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)

        self.rootPanel = wx.Panel(self)

        innerPanel = wx.Panel(self.rootPanel,-1, size=(150,150), style=wx.ALIGN_CENTER)
        innerPanel.SetBackgroundColour('WHITE')
        hbox = wx.BoxSizer(wx.HORIZONTAL) 
        vbox = wx.BoxSizer(wx.VERTICAL)
        innerBox = wx.BoxSizer(wx.VERTICAL)

        # I want this line visible in the CENTRE of the inner panel
        txt = wx.StaticText(innerPanel, id=-1, label="TEXT HERE",style=wx.ALIGN_CENTER, name="")
        innerBox.AddSpacer((150,75))
        innerBox.Add(txt, 0, wx.CENTER)
        innerBox.AddSpacer((150,75))
        innerPanel.SetSizer(innerBox)

        hbox.Add(innerPanel, 0, wx.ALL|wx.ALIGN_CENTER)
        vbox.Add(hbox, 1, wx.ALL|wx.ALIGN_CENTER, 5)

        self.rootPanel.SetSizer(vbox)
        vbox.Fit(self)

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'wxBoxSizer.py')
        frame.Show(True)
        frame.Center()
        return True

app = MyApp(0)
app.MainLoop()

谢谢你,迈克。我花了很长时间试图做到这一点,间隔方法很好。但是,我们不禁觉得应该有一个单行程序来完成这样的事情。好吧,您可以创建间隔符,然后使用sizer的AddMany()方法来添加所有三个。可能有一个我忘记的包装器,或者SizedControls可以工作。不过,我还没有真正使用后者。