wxPython对齐小部件和拟合

时间:2014-01-09 17:16:08

标签: python widget wxpython sizer

其实我有两个问题:

  1. 如何防止self.Fit()重新调整窗口小部件的大小?
  2. 这是没有.Fit()的窗口,我对TextCtrl的宽度非常满意,但我希望窗口的高度适应小部件。

    Imgur

    当我添加.Fit()时,TextCtrl的宽度会改变:

    With Fit With Fit

    1. 如何对齐两个TextCtrl,因为您可以注意到它们的起点不同,因为我试图使用wx.FlexGridSizer,但我无法调整StaticText和TextCtrl之间的间距
    2. 以下是代码:

      #!/usr/bin/env python
      import wx
      
      
      class loginWindow(wx.Frame):
          def __init__(self, parent, title):
              super(loginWindow, self).__init__(parent, title=title,
                                            style=wx.SYSTEM_MENU |
                                            wx.CAPTION |
                                            wx.CLOSE_BOX)
              self.InitUI()
              self.Center()
              self.Show()
      
          def InitUI(self):
              self.main_sizer = wx.BoxSizer(wx.VERTICAL)
              #User name
              self.hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
              self.user_name_lbl = wx.StaticText(self, label="User name:")
              self.hsizer1.Add(self.user_name_lbl, flag=wx.EXPAND | wx.RIGHT | wx.ALIGN_CENTER, border=5)
              self.user_name_txt = wx.TextCtrl(self)
              self.hsizer1.Add(self.user_name_txt, flag=wx.EXPAND | wx.LEFT, proportion=3, border=5)
              #Password
              self.hsizer2 = wx.BoxSizer(wx.HORIZONTAL)
              self.password_lbl = wx.StaticText(self, label="Password:")
              self.hsizer2.Add(self.password_lbl, flag=wx.EXPAND | wx.RIGHT | wx.ALIGN_CENTER, border=5)
              self.password_txt = wx.TextCtrl(self)
              self.hsizer2.Add(self.password_txt, flag=wx.EXPAND | wx.LEFT, proportion=3, border=5)
              #Login button
              self.login_button = wx.Button(self, label="Login")
              #Main sizer
              self.main_sizer.Add(self.hsizer1, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
              self.main_sizer.Add(self.hsizer2, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
              self.main_sizer.Add(self.login_button, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
              self.SetSizer(self.main_sizer)
              self.Fit()
      
      if __name__ == '__main__':
          app = wx.App()
          loginWindow(None, title="AppTitle")
          app.MainLoop()
      

2 个答案:

答案 0 :(得分:2)

防止self.Fit()重新调整宽度上的小部件

self.SetMinSize((self.WIDTH, -1))

通过设置最小宽度值,但给出高度为-1将固定宽度,但保留高度可由self.Fit()

设置

如果要将宽度固定为当前窗口宽度,请使用以下命令:

width, _ = self.GetSize()
self.SetMinSize((width, -1))

要对齐两个TextCtrl,请使用flexgrid sizer并给出标签样式wx.ALIGN_RIGHT

要使TextCtrl拉伸给它们样式wx.EXPAND并设置coloumn位置以便能够与

一起成长
FlexGridSizer.AddGrowableCol(col)

我还发现有一个框架 - > sizer - >面板 - > sizer - >控件使sizer更好地工作。

这是一些完整的代码

#!/usr/bin/env python
import wx


class loginWindow(wx.Frame):
    def __init__(self, parent, title):
        super(loginWindow, self).__init__(parent, title=title,
            style=wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
        self.WIDTH = 400
        self.InitUI()
        self.Center()
        self.Show()

    def InitUI(self):

        panel = wx.Panel(self)
        self.user_name_lbl = wx.StaticText(panel, label="User name:")
        self.user_name_txt = wx.TextCtrl(panel)
        self.password_lbl = wx.StaticText(panel, label="Password:")
        self.password_txt = wx.TextCtrl(panel)
        self.login_button = wx.Button(panel, label="Login")

        fg_sizer = wx.FlexGridSizer(cols=2, vgap=5, hgap=5)
        fg_sizer.Add(self.user_name_lbl, 0, wx.ALIGN_RIGHT | wx.ALIGN_BOTTOM)
        fg_sizer.Add(self.user_name_txt, 0, wx.EXPAND)
        fg_sizer.Add(self.password_lbl, 0, wx.ALIGN_RIGHT | wx.ALIGN_BOTTOM)
        fg_sizer.Add(self.password_txt, 0, wx.EXPAND)
        fg_sizer.AddGrowableCol(1)

        panel_sizer = wx.BoxSizer(wx.VERTICAL)
        panel_sizer.Add(fg_sizer, 0, wx.ALL | wx.EXPAND, 5)
        panel_sizer.Add(self.login_button, 0, wx.EXPAND | wx.ALL, 5)
        panel.SetSizer(panel_sizer)

        frame_sizer = wx.BoxSizer(wx.VERTICAL)
        frame_sizer.Add(panel, 1, wx.EXPAND)
        self.SetSizer(frame_sizer)
        self.SetMinSize((self.WIDTH, -1))
        self.Fit()


if __name__ == '__main__':
    app = wx.App()
    loginWindow(None, title="AppTitle")
    app.MainLoop()

答案 1 :(得分:1)

使StaticTexts具有相同的宽度...事实上设置所有宽度,这将解决您的所有问题

label_width = 125
text_ctrl_width= 265
...
self.user_name_lbl = wx.StaticText(self, label="User name:",size=(label_width,-1)
self.user_name_txt = wx.TextCtrl(self,size=(text_ctrl_width,-1)
...
self.password_lbl = wx.StaticText(self, label="Password:",size=(label_width,-1))
self.password_txt = wx.TextCtrl(self,size=(text_ctrl_width,-1)
...

粗糙的self.fit将改变你使用EXPAND样式的任何小部件的大小...降低到它的最小尺寸...所以你需要设置那个宽度,如果你想要它是那个宽度