我使用wxPython在Python中创建一个实用程序,但是我无法将方法绑定到按钮。
有趣的是,我已经以完全相同的方式绑定了另一个按钮(self.add_employee_to_list
),但是那个按钮有效。
class MyDialog(wx.Frame):
def __init__(self, parent, title):
self.no_resize = wx.DEFAULT_FRAME_STYLE & ~ (wx.RESIZE_BORDER | wx.MAXIMIZE_BOX)
wx.Frame.__init__(self, parent, title=title, size=(500, 450),style = self.no_resize)
self.panel = wx.Panel(self, size=(250, 270))
self.add_employee_to_list = wx.Button(self.panel, label="Add Employee", pos=(250,50), size=(115,25))
self.add_employee_to_list.Bind(wx.EVT_BUTTON, self.add_employee_to_box)
#Button that produces error on binding
self.remove_employee_from_list = wx.Button(self.panel, label="Remove Employee",pos= (250,80), size=(115,25))
self.remove_employee_from_list.Bind(wx.EVT_BUTTON, self.remove_employee_from_list)
def remove_employee_from_list(self, event):
pass
app = wx.App(False)
frame = MyDialog(None, "Crystal Rose")
app.MainLoop()
我收到以下错误:
Traceback (most recent call last): File "C:/Users/SR/PycharmProjects/CrystalRose/CrystalRoseGUI.py", line 59, in <module>
frame = MyDialog(None, "Crystal Rose") File "C:/Users/SR/PycharmProjects/CrystalRose/CrystalRoseGUI.py", line 22, in __init__
self.remove_employee_from_list.Bind(wx.EVT_BUTTON, self.remove_employee_from_list) File "C:\Program Files (x86)\Python36-32\lib\site-packages\wx\core.py", line 1339, in
_EvtHandler_Bind
assert callable(handler) or handler is None AssertionError
答案 0 :(得分:1)
您将remove_employee_from_list
声明为方法,并将其设置为__init__
中的按钮。触发的按钮和方法不一样,需要不同的名称。试试这个:
import wx
class MyDialog(wx.Frame):
def __init__(self, parent, title):
self.no_resize = wx.DEFAULT_FRAME_STYLE & ~ (wx.RESIZE_BORDER | wx.MAXIMIZE_BOX)
wx.Frame.__init__(self, parent, title=title, size=(500, 450),style = self.no_resize)
self.panel = wx.Panel(self, size=(250, 270))
self.add_employee_to_list = wx.Button(self.panel, label="Add Employee", pos=(250,50), size=(115,25))
self.add_employee_to_list.Bind(wx.EVT_BUTTON, self.add_employee_to_box)
#Button that produces error on binding
self.remove_employee_from_list = wx.Button(self.panel, label="Remove Employee",pos= (250,80), size=(115,25))
self.remove_employee_from_list.Bind(wx.EVT_BUTTON, self.remove_employee_from_box)
def remove_employee_from_box(self, event):
pass
def add_employee_to_box(self, event):
pass
app = wx.App(False)
frame = MyDialog(None, "Crystal Rose")
frame.Show()
app.MainLoop()