其实我有两个问题:
- 如何防止 self.Fit() 重新调整小部件的大小?
这是没有 .Fit() 的窗口,我对 TextCtrl 的宽度非常满意,但我希望窗口的高度能够适应小部件。
当我添加 .Fit() 时,TextCtrl 的宽度会发生变化:
- 如何对齐两个 TextCtrl,因为您可以注意到它们的起点不同,因为我尝试使用 wx.FlexGridSizer,但在调整 StaticText 和 TextCtrl 之间的间距时遇到了麻烦
这是代码:
#!/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()