wxpython:EVT\u网格\u单元\u已更改问题
我已经使用wx.grid创建了一个网格,绑定了wxpython:EVT\u网格\u单元\u已更改问题,python,wxpython,Python,Wxpython,我已经使用wx.grid创建了一个网格,绑定了EVT\u grid\u CELL\u CHANGED,以验证用户在单元格中的输入 self.grid.Bind(wx.grid.EVT_GRID_CELL_CHANGED, self.OnCellChanged) 如果输入不是int,我尝试创建一个wx.MessageBox弹出。然后我 发现messagebox会弹出两次,这不是我想要的 处理程序的代码如下所示: def OnCellChanged(self, event): row =
EVT\u grid\u CELL\u CHANGED
,以验证用户在单元格中的输入
self.grid.Bind(wx.grid.EVT_GRID_CELL_CHANGED, self.OnCellChanged)
如果输入不是int
,我尝试创建一个wx.MessageBox
弹出。然后我
发现messagebox会弹出两次,这不是我想要的
处理程序的代码如下所示:
def OnCellChanged(self, event):
row = event.GetRow()
col = event.GetCol()
try:
cell_input = int(self.grid.GetCellValue(row, col))
except:
self.grid.SetCellValue(row, col, '')
msgbox = wx.MessageBox('Invalid Input! Please Try Again', 'Error', wx.OK | wx.ICON_HAND | wx.CENTRE)
感谢您的帮助。处理事件的函数中的
MessageBox
似乎有一些问题。但是您可以稍后使用wx.CallAfter(函数名称)
或wx.CallLater(毫秒,函数名称)
完整的工作示例(基于示例:)
(可能)当您将
SetCellValue
与新值一起使用时,您会更改单元格中的值,以便wxpython创建事件EVT\u GRID\u cell\u CHANGED
,并再次调用OnCellChanged()
。int()
raiseexception中的空字符串,您将获得第二个MessageBox
@furas我删除了self.grid.SetCellValue(行,列,')
,但仍然得到了相同的结果。不管怎样,谢谢你的建议,效果很好。
def OnCellChanged(self, event):
row = event.GetRow()
col = event.GetCol()
try:
cell_input = int(self.grid.GetCellValue(row, col))
except:
self.SetCellValue(row, col, '')
#wx.CallLater(100, self.Later) # time 100ms
wx.CallAfter(self.Later)
print("End OnCellChange")
def Later(self):
print("Later")
msgbox = wx.MessageBox('Invalid Input! Please Try Again', 'Error', wx.OK | wx.ICON_HAND | wx.CENTRE)
import wx
import wx.grid as gridlib
class MyGrid(gridlib.Grid):
def __init__(self, parent):
"""Constructor"""
gridlib.Grid.__init__(self, parent)
self.CreateGrid(12, 8)
self.Bind(gridlib.EVT_GRID_CELL_CHANGED, self.OnCellChange)
def OnCellChange(self, evt):
print("OnCellChange: (%d,%d) %s\n" % (evt.GetRow(), evt.GetCol(), evt.GetPosition()))
row = evt.GetRow()
col = evt.GetCol()
val = self.GetCellValue(row, col)
try:
cell_input = int(val)
except:
self.SetCellValue(row, col, '')
#wx.CallLater(100, self.Later) # time 100ms
wx.CallAfter(self.Later)
print("End OnCellChanged")
def Later(self):
print("Later")
wx.MessageBox('Invalid Input! Please Try Again', 'Error', wx.OK | wx.ICON_HAND | wx.CENTRE)
class MyForm(wx.Frame):
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, parent=None, title="An Eventful Grid")
panel = wx.Panel(self)
myGrid = MyGrid(panel)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(myGrid, 1, wx.EXPAND)
panel.SetSizer(sizer)
app = wx.App()
frame = MyForm().Show()
app.MainLoop()