Wxpython回调
我的框架中有3个控件-Wxpython回调,wxpython,Wxpython,我的框架中有3个控件- 包含员工表中员工姓名列表的列表框 接受新员工姓名的文本框 单击一个命令按钮将在employee表中插入一个新名称 要求: 一旦我在插入新行后按下submit按钮,列表框就会自动用新名称刷新 我如何完成这项任务 我成功地创建了控件,绑定了一个点击事件并插入了一行。但无法刷新列表框 提前感谢您的帮助。您应该使用列表框的SetItems方法: import wx #########################################################
提前感谢您的帮助。您应该使用列表框的SetItems方法:
import wx
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
self.choices = ["George Lucas"]
self.lbox = wx.ListBox(self, choices=self.choices)
self.new_emp = wx.TextCtrl(self)
addBtn = wx.Button(self, label="Add Employee")
addBtn.Bind(wx.EVT_BUTTON, self.addEmployee)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.lbox, 0, wx.ALL|wx.EXPAND, 5)
sizer.Add(self.new_emp, 0, wx.ALL|wx.EXPAND, 5)
sizer.Add(addBtn, 0, wx.ALL, 5)
self.SetSizer(sizer)
#----------------------------------------------------------------------
def addEmployee(self, event):
""""""
emp = self.new_emp.GetValue()
self.choices.append(emp)
self.lbox.SetItems(self.choices)
self.new_emp.SetValue("")
########################################################################
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Employee")
panel = MyPanel(self)
self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
app.MainLoop()
您应该使用ListBox的SetItems方法:
import wx
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
self.choices = ["George Lucas"]
self.lbox = wx.ListBox(self, choices=self.choices)
self.new_emp = wx.TextCtrl(self)
addBtn = wx.Button(self, label="Add Employee")
addBtn.Bind(wx.EVT_BUTTON, self.addEmployee)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.lbox, 0, wx.ALL|wx.EXPAND, 5)
sizer.Add(self.new_emp, 0, wx.ALL|wx.EXPAND, 5)
sizer.Add(addBtn, 0, wx.ALL, 5)
self.SetSizer(sizer)
#----------------------------------------------------------------------
def addEmployee(self, event):
""""""
emp = self.new_emp.GetValue()
self.choices.append(emp)
self.lbox.SetItems(self.choices)
self.new_emp.SetValue("")
########################################################################
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Employee")
panel = MyPanel(self)
self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
app.MainLoop()