wxPython Sizer未添加内容

问题描述 投票:0回答:1

我是wxPython的新手,可以使用一些帮助。

我已经创建了一个面板,其中包含一些使用大小调整器及其内容的内容。但是我添加了一个按钮单击事件,该事件应该将内容添加到sizer。但是,当我单击按钮时,什么都没有出现,尽管我没有收到错误消息。帮助将不胜感激。我尝试过在面板上使用self.Refresh(),self.Update()等等,但没有任何效果。

应该发生的事情是,当您按下创建的加号按钮时,它在其下方创建了一个新的加号/减号按钮。当我单击初始加号按钮时,我在终端上看到“加号”并且没有错误,但是新按钮没有出现。

谢谢

类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)

类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)
python wxpython
1个回答
0
投票

您要在CalcPanel中添加到sizer中,然后尝试self.Refresh()等,但是在这种情况下,self是CalcPanel而不是Main Frame,您在其中还分配了sizer并放入其中CalcPanel 。更改发生在CalcPanel尺寸调整器中,但您看不到它,因为它本身就是在父级(框架)尺寸调整器中。尝试:

self.parent.Layout()
© www.soinside.com 2019 - 2024. All rights reserved.