gpt4 book ai didi

python - wxPython 对齐小部件和拟合

转载 作者:太空宇宙 更新时间:2023-11-04 01:21:15 24 4
gpt4 key购买 nike

其实我有两个问题:

  1. 如何防止 self.Fit() 调整小部件的大小?

这是没有 .Fit() 的窗口,我对我的 TextCtrl 的宽度很满意,但我希望窗口的高度能够根据小部件进行调整。

Imgur

当我添加 .Fit() 时,TextCtrl 的宽度发生变化:

With Fit With Fit

  1. 如何对齐两个 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()

最佳答案

防止 self.Fit() 仅在宽度上调整小部件的大小

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

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

如果你想宽度固定为当前窗口宽度,使用如下:

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

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

要使 TextCtrl 拉伸(stretch),请给它们样式 wx.EXPAND 并设置那里的列位置以便能够随

FlexGridSizer.AddGrowableCol(col)

我还发现有一个 frame -> sizer -> panel -> sizer -> controls 可以让 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()

关于python - wxPython 对齐小部件和拟合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21026864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com