wxpython:EVT_GRID_CELL_CHANGED 问题
wxpython:EVT_GRID_CELL_CHANGED issue
我使用 wx.grid 创建了一个网格,与 EVT_GRID_CELL_CHANGED
绑定以验证用户在单元格中的输入。
self.grid.Bind(wx.grid.EVT_GRID_CELL_CHANGED, self.OnCellChanged)
我尝试创建一个 wx.MessageBox
以在输入不是 int
时弹出。然后我
发现消息框会弹出两次,这不是我想要的。
handler的代码如下:
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(function_name)
或 wx.CallLater(miliseconds, function_name)
调用它
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)
完整的工作示例(基于示例:Mouse vs Python: wxPython - An Introduction to Grids)
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()
我使用 wx.grid 创建了一个网格,与 EVT_GRID_CELL_CHANGED
绑定以验证用户在单元格中的输入。
self.grid.Bind(wx.grid.EVT_GRID_CELL_CHANGED, self.OnCellChanged)
我尝试创建一个 wx.MessageBox
以在输入不是 int
时弹出。然后我
发现消息框会弹出两次,这不是我想要的。
handler的代码如下:
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(function_name)
或 wx.CallLater(miliseconds, function_name)
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)
完整的工作示例(基于示例:Mouse vs Python: wxPython - An Introduction to Grids)
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()