【发布时间】:2020-03-27 11:40:08
【问题描述】:
我正在尝试向网格单元格添加一个按钮。
我正在使用最新的演示 4.0.7 和 python 3.7(32 位)
我修复了一个旧示例:https://groups.google.com/forum/#!topic/wxpython-users/HhtKCxPVX_s
问题是按钮无法正常工作,按下它会导致整个网格消失。
class MyCustomRenderer2(gridlib.GridCellRenderer):
def __init__(self):
gridlib.GridCellRenderer.__init__(self)
self.down = False
self.click_handled = False
def Draw(self, grid, attr, dc, rect, row, col, isSelected):
"""This is called when the widget is Refreshed"""
print ('drawing button')
dc.Clear()
if self.down:
state = wx.CONTROL_PRESSED | wx.CONTROL_SELECTED
else:
state = 0
#if not self.IsEnabled():
# state = wx.CONTROL_DISABLED
#pt = self.ScreenToClient(wx.GetMousePosition())
#if self.GetClientRect().Contains(pt):
# state |= wx.CONTROL_CURRENT
wx.RendererNative.Get().DrawPushButton(grid, dc, rect, state)
#extra logic required since a button gets drawn at various times that could be while the mouse button is held down
if self.down and not self.click_handled:
self.click_handled = True
self.HandleClick()
def HandleClick(self):
print ('clicked')
def GetBestSize(self, grid, attr, dc, row, col):
text = grid.GetCellValue(row, col)
dc.SetFont(attr.GetFont())
w, h = dc.GetTextExtent(text)
return wx.Size(w, h)
def Clone(self):
return MyCustomRenderer2()
【问题讨论】:
标签: wxpython