简体   繁体   中英

wxPython Sizer not adding content

I'm new to wxPython and could use some help.

I've created a panel with some content using sizers and its fine. However i added a button click event that is supposed to add content to a sizer. However when i click the button, nothing appears, although I'm not getting an error message. Help would be appreciated. I've tried using self.Refresh(), self.Update() on the panel among other things to no effect.

Whats supposed to happen is when you hit the plus button initialy created, it creates a new air of plus/minus buttons underneath it. When i click the initial plus button, i see "PLUS" in the terminal and no errors, but the new buttons do not appear.

Thanks

class MyFrame(wx.Frame):

def __init__(self, *args, **kwargs):

    # ini the frame and show it
    super(MyFrame, self).__init__(*args, **kwargs)

    # main sizer, will contain all of the other sizers
    self.main_sizer = wx.BoxSizer(wx.VERTICAL)

    # FONTS
    txt_font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
    but_font = wx.Font(15, wx.DEFAULT, wx.NORMAL, wx.NORMAL)

    # PANEL 3: calculate button and options
    self.calc_panel = CalcPanel(self, txt_font, but_font)
    self.main_sizer.Add(self.calc_panel, proportion=0, flag=wx.EXPAND|wx.ALL, border=10)

    self.SetSizer(self.main_sizer)

    # show the frame with panel
    self.Show(True)

class CalcPanel(wx.Panel):

def __init__(self, parent, txt_font, but_font):

    super().__init__(parent=parent)

    # set background colour for panel
    self.SetBackgroundColour("Orange")
    self.parent = parent

    self.calc_panel_sizer = wx.BoxSizer(wx.VERTICAL)

    self.but_select_bg_color_on = (181, 194, 35, 255)
    self.but_select_bg_color_off = (240, 240, 240, 255)

    # just start with a + and - button to add/remove needs
    plus_minus_need_sizer = wx.BoxSizer(wx.HORIZONTAL)

    # plus button
    plus_need_button = wx.Button(self, -1, size=(75, 25))
    plus_need_button.SetLabel("+")    
    plus_need_button.Bind(wx.EVT_BUTTON, self.plus_need_button)  
    plus_minus_need_sizer.Add(plus_need_button, proportion=0, flag=wx.EXPAND)

    # minus button
    minus_need_button = wx.Button(self, -1, size=(75, 25))
    minus_need_button.SetLabel("-")    
    minus_need_button.Bind(wx.EVT_BUTTON, self.minus_need_button)  
    plus_minus_need_sizer.Add(minus_need_button, proportion=0, flag=wx.EXPAND)

    # add +/-buttons to panel
    self.calc_panel_sizer.Add(plus_minus_need_sizer, proportion=0, flag=wx.EXPAND)

    # set the panel sizer
    self.SetSizer(self.calc_panel_sizer)


# add a new need by clicking + button
def plus_need_button(self, e):

    print("PLUS")

    need_sizer = wx.BoxSizer(wx.HORIZONTAL)

    # plus button
    inline_plus_button = wx.Button(self, -1, size=(30, 100))
    inline_plus_button.SetLabel("+")    
    inline_plus_button.Bind(wx.EVT_BUTTON, self.plus_need_button)  
    need_sizer.Add(inline_plus_button, proportion=0, flag=wx.EXPAND)

    # minus button
    inline_minus_button = wx.Button(self, -1, size=(30, 100))
    inline_minus_button.SetLabel("+")    
    inline_minus_button.Bind(wx.EVT_BUTTON, self.minus_need_button)  
    need_sizer.Add(inline_minus_button, proportion=0, flag=wx.EXPAND)

    self.calc_panel_sizer.Add(need_sizer, proportion=0, flag=wx.EXPAND)

You are adding to the sizer in CalcPanel and then attempting self.Refresh() etc but self in this case is the CalcPanel and not the Main Frame, where you also assigned a sizer and dropped into it CalcPanel .
The change is happening in the CalcPanel sizer but you don't see it because that is itself, in the parent's (the frame) sizer.
try:

self.parent.Layout()

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