简体   繁体   中英

wxPython: Resizing frame to match sizer

I am trying to create a floating button bar in wxPython using a wx.frame. I have started with 2 buttons as a prototype, but I can't get the frame itself to resize. Is panel.SetSizerAndFit(sizer) the correct statement to use?

import wx

class MainToolbar(wx.Frame):
    def __init__(self):
        super(MainToolbar, self).__init__(None, title='some title')

        panel = wx.Panel(self)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        buttonSizer = wx.GridSizer(rows=1, cols=2, vgap=1, hgap=1)
        btn1 = wx.Button(panel, label='Ok', size=(100,100))
        btn2 = wx.Button(panel, label='Close', size=(100,100))
        buttonsArray = [ (btn1, 0, wx.EXPAND), (btn2, 0, wx.EXPAND) ]
        buttonSizer.AddMany(buttonsArray)
        sizer.Add(buttonSizer, proportion=1, flag=wx.EXPAND)
        panel.SetSizerAndFit(sizer)

if __name__ == "__main__":
    app = wx.App()
    frame = MainToolbar()
    frame.Show()
    app.MainLoop()

The result from this is: 在此处输入图像描述

Putting widgets in the sizers with proportion=1 and EXPAND flags, is going to fill the available space.
Also, I'm not sure what wxPython does when you specify a size and put the widget in a sizer , I assume it runs with the implicit size instruction.

Clearly, I can't guess how you wish the widgets to react, if you resize the window but here's a bare bones way of getting it all to fit.

import wx

class MainToolbar(wx.Frame):
    def __init__(self):
        super(MainToolbar, self).__init__(None, title='some title')

        panel = wx.Panel(self)
        buttonSizer = wx.GridSizer(rows=1, cols=2, vgap=1, hgap=1)
        btn1 = wx.Button(panel, label='Ok', size=(100,100))
        btn2 = wx.Button(panel, label='Close', size=(100,100))
        buttonsArray = [ (btn1), (btn2) ]
        buttonSizer.AddMany(buttonsArray)
        panel.SetSizer(buttonSizer)
        mainsizer = wx.BoxSizer(wx.VERTICAL)
        mainsizer.Add(panel)
        self.SetSizerAndFit(mainsizer)
        self.Show()

if __name__ == "__main__":
    app = wx.App()
    frame = MainToolbar()
    app.MainLoop()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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