简体   繁体   English

wxPython对齐小部件并进行拟合

[英]wxPython align widgets and fitting

Actually I have two questions: 其实我有两个问题:

  1. How to prevent self.Fit() from re sizing the widgets? 如何防止self.Fit()调整窗口小部件的大小?

Here is the window without .Fit(), I'm quite happy with the width of my TextCtrl, but I want the height of the window to adjust to the widgets. 这是没有.Fit()的窗口,我对我的TextCtrl的宽度感到非常满意,但是我希望窗口的高度能够适应小部件。

Imgur

When I add .Fit(), the width of TextCtrl changes: 当我添加.Fit()时,TextCtrl的宽度发生变化:

With Fit 适合 适合

  1. How do I align the two TextCtrl, as you can notice their starting point is different, for that I tried to use a wx.FlexGridSizer, but I had trouble adjusting the spacing between the StaticText and the TextCtrl 我如何对齐两个TextCtrl,您会注意到它们的起点是不同的,因为我尝试使用wx.FlexGridSizer,但是在调整StaticText和TextCtrl之间的间距时遇到了麻烦

Here is the code: 这是代码:

#!/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()

To prevent self.Fit() from re sizing the widgets on the width only 为了防止self.Fit()仅调整宽度上的小部件大小

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

By setting a min width value, but giving a height of -1 will fix the width but leave the height free to be set by self.Fit() 通过设置最小宽度值,但将高度设置为-1将固定宽度,但高度可以自由设置为self.Fit()

If you want the width fixed to the current window width, use the following: 如果要将宽度固定为当前窗口的宽度,请使用以下命令:

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

To align the two TextCtrl use a flexgrid sizer and give the labels style wx.ALIGN_RIGHT 要对齐两个TextCtrl,请使用flexgrid sizer,并将标签样式设置为wx.ALIGN_RIGHT

To make the TextCtrl stretch give them the style wx.EXPAND and set there coloumn position to be able to grow with 为了使TextCtrl拉伸,请给他们样式wx.EXPAND,并在其中设置要与之一起成长的位置

FlexGridSizer.AddGrowableCol(col)

I also find that having a frame -> sizer -> panel -> sizer -> controls makes sizers work better. 我还发现拥有框架-> sizer->面板-> sizer->控件使sizer更好地工作。

Here is some complete code 这是一些完整的代码

#!/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()

make the StaticTexts the same width ... in fact set all the widths, and that will solve all your problems 使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)
...

of coarse self.fit is going to change the size of any widget that you use the EXPAND style on ... down to its minimal size ... so you need to set that width if you want it to be that width self.fit的粗略设置将改变您使用EXPAND样式的所有小部件的大小...减小到最小大小...因此,如果您希望将其设置为该宽度,则需要设置该宽度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM