【发布时间】:2020-06-14 22:02:12
【问题描述】:
我是 wxPython 的新手,需要一些帮助。
我使用 sizer 创建了一个包含一些内容的面板,它很好。但是,我添加了一个按钮单击事件,该事件应该将内容添加到 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)
【问题讨论】: