###### python3.6
import wx
import wx.grid as gridlib
class BaiTan_Frame(wx.Frame):
def __init__(self, parent, title):
super(BaiTan_Frame, self).__init__(parent, title = title)
self.InitUI(None, 0, 0, None)
self.Centre()
self.Show()
def InitUI(self, n_grid, iRow, iCol, const):
panel = wx.Panel(self)
sizer = wx.GridBagSizer(0, 0)
panel.SetSize((1600, 1400))
self.SetSize((830,300))
# 开始设置表格 tc_grid
tc_grid = gridlib.Grid(panel)
tc_grid.CreateGrid(6, 8)# 设置表格行列数
sizer.Add(tc_grid, pos = (0, 0), span=(0, 6), flag = wx.ALL, border = 5)
tc_grid.SetRowLabelSize(120)# 固定列的宽度
# #=======================
tc_grid.SetColLabelSize(0)# 固定行的高度 隐藏
tc_grid.SetColLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )# 行表头内容对齐方式
tc_grid.SetDefaultCellAlignment( wx.ALIGN_CENTRE, wx.ALIGN_TOP )# 表格内容对齐方式
if n_grid == 'tc_grid':
tc_grid.SetCellValue(iRow, iCol, const)
#=============
renderer = wx.grid.GridCellAutoWrapStringRenderer()
# 开始设置按钮
button1 = wx.Button(panel, label = "按钮1")
self.Bind(wx.EVT_BUTTON, self.button1_Click, button1)
button2 = wx.Button(panel, label = "按钮2" )
self.Bind(wx.EVT_BUTTON, self.button2_Click, button2)
buttonExit = wx.Button(panel, label = "退出" )
self.Bind(wx.EVT_BUTTON, self.buttonExit_Click, buttonExit)
sizer.Add(button1, pos = (2, 0),flag = wx.ALL, border = 5) # pos 与位置有关
sizer.Add(button2, pos = (2, 1), flag = wx.ALL, border = 5) # pos 与位置有关
sizer.Add(buttonExit, pos = (2, 5),flag = wx.ALL, border = 5) # pos 与位置有关
panel.SetSizerAndFit(sizer)
def button1_Click(self, event):
self.InitUI('tc_grid', 2, 4, 'Python')
def button2_Click(self, event):
self.InitUI('tc_grid', 3, 5, '你好')
def buttonExit_Click(self, event):
self.Close(True)
app = wx.App()
BaiTan_Frame(None, title = 'GridBag Demo')
app.MainLoop()
######
#按钮1显示 "Python" ,按按钮2会显示 "你好" 但会擦掉 "Python",反之亦然
怎么通过button修改grid的某个单元格而不影响其它单元格的内容
相似问题