wxpython:设置最小窗口宽度 / 调整大小时按钮消失
我有一个主窗口,里面有两个垂直排列的面板;上面的面板里有文本和按钮(按钮在面板内是水平排列的),而下面的面板则包含一些可以轻松调整大小的内容。我想设置这个窗口的最小宽度,这样当我把窗口缩小时,上面的按钮和其他内容不会被遮住,因为这些内容必须始终可见。经过大量搜索,似乎没有现成的wx函数可以做到这一点(我找到的最接近的办法是self.SetMinSize(self.GetSize()),但这也限制了窗口的高度,而我并不想这样)。我也想知道是否有方法可以设置窗口的最小高度(不过我想这和宽度的设置应该类似)。
另一种方法是找到一种方式,当按钮没有更多空间可以移动以适应窗口大小的变化时,阻止窗口宽度的进一步缩小(这有点像菜单栏,菜单会堆叠起来,直到没有更多空间为止)。
有没有简单的方法可以做到这一点,还是说这需要更多的代码来定制一个框架类,然后在主窗口中使用?谢谢!
这里是代码,以备不时之需:
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(850, 800))
self.exedir = os.getcwd()
self.dirname= ''
#self.SetMinSize(self.GetSize())
#Set up the initial frames.
self.panel1 = wx.Panel(self) #Create a new panel
self.panel2 = wx.Panel(self) #Create a panel for the resulting search
self.nb = wx.Notebook(self.panel2, size=(wx.EXPAND, 0))
mainTab = MainTab(self.nb)
self.nb.AddPage(mainTab, "Main")
#if page2 is not None:
#self.nb.AddPage(page2, "Employees")
#self.nb.Update(self)
#self.nb.AddPage(page2, "Employees")
#nb.AddPage(page3, "Tasks")
self.statusText = wx.StaticText(self.panel1, label="Status: ", size = (50,20))
self.result = wx.StaticText(self.panel1, label="No Connection", size = (575,20))
self.result.SetForegroundColour(wx.BLACK)
self.buttonConnect = wx.Button(self.panel1, label="Connect")
self.buttonDisconnect = wx.Button(self.panel1, label="Disconnect")
self.buttonDisconnect.Disable() #Originally disabled
self.CreateStatusBar() # A Statusbar in the bottom of the window
# Setting up the menu.
configmenu = wx.Menu()
configmenuConfig = configmenu.Append(wx.ID_PROPERTIES, "&Configuration"," Set up the database configuration")
taskmenu = wx.Menu()
taskmenuOpen = taskmenu.Append(wx.ID_OPEN, "&Fill Employee Task database from .csv"," Fill the entire database form a .csv file")
taskmenuSave = taskmenu.Append(wx.ID_SAVE, "&Save database to .csv"," Backup the database to a crv")
taskmenuNew = taskmenu.Append(wx.ID_NEW, "&New Project/Program"," Create a new Project/Program")
taskmenuChange = taskmenu.Append(wx.ID_REVERT, "&Change or delete a Project/Program"," Remove or Modify a Project/Program")
taskmenuViewTasks = taskmenu.Append(wx.ID_VIEW_LIST, "&View list of Employee Tasks"," View all the employee's tasks in the database")
helpmenu = wx.Menu()
helpmenuHelp = helpmenu.Append(wx.ID_HELP, "&Help"," Instructions on how to use program")
helpmenuAbout = helpmenu.Append(wx.ID_ABOUT, "&About"," Information about the program")
exitmenu = wx.Menu()
exitmenuExit = exitmenu.Append(wx.ID_EXIT, "&Exit"," Quit the program safely")
employeemenu = wx.Menu()
employeemenuAddAll = employeemenu.Append(wx.ID_REPLACE_ALL, "&Fill Employee database from .csv"," Fill the entire database form a .csv file")
employeemenuBackup = employeemenu.Append(wx.ID_DUPLICATE, "&Backup Employee database to .csv"," Backup the database to a .csv file")
employeemenuViewall = employeemenu.Append(wx.ID_VIEW_LIST, "&View list of Employees"," View all the employees in the database")
employeemenuAdd = employeemenu.Append(wx.ID_FIND, "&Add Employee"," Add Employee to the database")
employeemenuModify = employeemenu.Append(wx.ID_REPLACE, "&Modify Employee Fields"," Modify an existing Employee")
employeemenuDelete = employeemenu.Append(wx.ID_DELETE, "&Delete Employee","Remove and Employee from the database")
# Creating the menubar.
self.menuBar = wx.MenuBar()
self.menuBar.Append(configmenu,"&Config") # Adding the "configmenu" to the MenuBar
self.menuBar.Append(taskmenu,"&Task") # Adding the "Task" to the MenuBar
self.menuBar.Append(employeemenu,"&Employees") # Adding the "Employees" to the MenuBar
self.menuBar.Append(helpmenu,"&Help") # Adding the "Help" to the MenuBar
self.menuBar.Append(exitmenu,"&Quit") # Adding the "Quit" to the MenuBar
self.SetMenuBar(self.menuBar) # Adding the MenuBar to the Frame content.
self.menuBar.EnableTop(pos=2,enable=False) #Disable the EMPLOYEE menubar until it is ready
self.menuBar.EnableTop(pos=1,enable=False)
# Events for Config
self.Bind(wx.EVT_MENU, self.OnConfig, configmenuConfig)
#Set the sizer for the first panel
panelSizer = wx.BoxSizer(wx.HORIZONTAL)
panelSizer.Add(self.statusText, proportion=0)
panelSizer.Add(self.result, proportion=0)
panelSizer.Add(self.buttonConnect, proportion=0)
panelSizer.Add(self.buttonDisconnect, proportion=0)
self.panel1.SetSizer(panelSizer) #Set the panel1 sizer
self.panel1.Layout()
#Set the sizer for the second panel
panel2Sizer = wx.BoxSizer(wx.HORIZONTAL)
panel2Sizer.Add(self.nb, 0, wx.EXPAND)
#panel2Sizer.Add(self.grid, 1, wx.EXPAND)
self.panel2.SetSizerAndFit(panel2Sizer)
#self.panel2.Layout
# Set sizer for the main frame, so we can change frame size to match widgets
self.windowSizer = wx.BoxSizer(wx.VERTICAL)
self.windowSizer.Add(self.panel1, proportion=0)
self.windowSizer.Add(self.panel2, wx.EXPAND)
self.SetSizer(self.windowSizer)
self.Layout()
self.Show()
1 个回答
1
我觉得防止用户把窗口缩得太小的一个简单方法就是使用它的 SetSizeHints 方法。你只需要传入最小宽度/高度和最大宽度/高度。
这里有个简单的例子:
self.SetSizeHints(400,400,1200,1200)
你可以在 文档 中了解更多关于这个方法的信息。我还找到一个类似的问题,里面有一些有趣的信息: