wxpython:在调整大小时设置最小帧宽/按钮消失

时间:2014-07-11 17:18:29

标签: python resize wxpython

我有一个MainWindow框架,包含垂直对齐的面板;顶部包含文本和按钮(在面板内水平对齐),而底部包含可轻松调整大小的内容。我希望能够设置我的框架的最小宽度,这样当我调整它的大小时,它不会简单地取代我的顶部面板中的按钮和其他内容,它必须始终可见。经过广泛的搜索,似乎没有内置的wx函数(我发现最接近的是self.SetMinSize(self.GetSize()),但这也限制了窗口的高度,我不会这样做。我想)。我也很有兴趣知道是否有一种方法可以使用窗口高度(尽管我确定它与宽度相似)。

另一种方法是找到一种方法,一旦按钮没有更多空间移动以适应调整大小,它可以防止窗口宽度进一步减小(类似于菜单栏,它将堆叠下拉菜单,直到有没有更多的空间)。

是否有一种简单的方法可以解决这个问题,或者这需要更广泛的硬编码到自定义框架类并由MainWindow使用?谢谢!

以下是以下代码:

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 个答案:

答案 0 :(得分:1)

我认为阻止用户使框架太小的最简单方法之一是使用 SetSizeHints 方法。您只需传递最小宽度/高度和最大宽度/高度。

这是一个简单的例子:

self.SetSizeHints(400,400,1200,1200)

您可以在documentation中详细了解此方法。我还发现了另一个类似的问题,其中包含一些有趣的信息: