我正在尝试显示一个网格以及一些按钮,用于使用 wx.Boxsizer 在 wx.Panel 上导航我的表格。如果我首先将按钮添加到 BoxSizer 中,其中按钮在垂直布局中位于网格顶部,我可以让网格和按钮正确显示。如果我在垂直布局中切换网格在按钮顶部的顺序,按钮将不会显示网格将占据整个面板。
作品:
self.sizer.AddSpacer(10)
self.sizer.Add(self.bottomBtnsPnl,1,wx.EXPAND)
self.sizer.AddSpacer(10)
self.sizer.Add(self.gridPnl,1,wx.EXPAND)
self.sizer.AddSpacer(10)
不起作用:
self.sizer.AddSpacer(10)
self.sizer.Add(self.gridPnl,1,wx.EXPAND)
self.sizer.AddSpacer(10)
self.sizer.Add(self.bottomBtnsPnl,1,wx.EXPAND)
self.sizer.AddSpacer(10)
我所有的代码:TableView.py:
import wx
from TFMtable import TFMemployeeGridTable
class TableView(wx.Panel):
def __init__(self, parent, ID, db):
wx.Panel.__init__(self, parent, ID)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.gridPnl = wx.Panel(self,1)
self.gridPnlSizer = wx.BoxSizer(wx.VERTICAL)
if db.getTFMemployeeRowCount() >= 100:
self.TFMemployeeGrid = TFMemployeeGridTable(self.gridPnl, gridRows=db.getTFMemployeeRowCount(), rows=db.getTFMemployee100Rows(0,100))
else:
self.TFMemployeeGrid = TFMemployeeGridTable(self.gridPnl, gridRows=db.getTFMemployeeRowCount(), rows=db.getTFMemployee100Rows(0,db.getTFMemployeeRowCount()))
self.gridPnlSizer.Add(self.TFMemployeeGrid,1,wx.EXPAND)
self.gridPnl.SetAutoLayout(True)
self.gridPnl.SetSizer(self.gridPnlSizer)
self.gridPnlSizer.Fit(self.gridPnl)
self.bottomBtnsPnl = wx.Panel(self,1)
self.bottomSizer = wx.BoxSizer(wx.HORIZONTAL)
self.beginningBtn =wx.Button(self.bottomBtnsPnl, 0, label='<<')
self.backBtn =wx.Button(self.bottomBtnsPnl, 0, label='<')
self.editBtn =wx.Button(self.bottomBtnsPnl, 0, label='Edit')
self.nextBtn =wx.Button(self.bottomBtnsPnl, 0, label='>')
self.lastBtn =wx.Button(self.bottomBtnsPnl, 0, label='>>')
self.bottomSizer.AddStretchSpacer()
self.bottomSizer.Add(self.beginningBtn,0,wx.EXPAND)
self.bottomSizer.AddSpacer(10)
self.bottomSizer.Add(self.backBtn,0,wx.EXPAND)
self.bottomSizer.AddSpacer(10)
self.bottomSizer.Add(self.editBtn,0,wx.EXPAND)
self.bottomSizer.AddSpacer(10)
self.bottomSizer.Add(self.nextBtn,0,wx.EXPAND)
self.bottomSizer.AddSpacer(10)
self.bottomSizer.Add(self.lastBtn,0,wx.EXPAND)
self.bottomSizer.AddStretchSpacer()
self.bottomBtnsPnl.SetAutoLayout(True)
self.bottomBtnsPnl.SetSizer(self.bottomSizer)
self.bottomSizer.Fit(self.bottomBtnsPnl)
self.sizer.AddSpacer(10)
self.sizer.Add(self.gridPnl,1,wx.EXPAND)
self.sizer.AddSpacer(10)
self.sizer.Add(self.bottomBtnsPnl,1,wx.EXPAND)
self.sizer.AddSpacer(10)
self.SetAutoLayout(True)
self.SetSizer(self.sizer)
self.sizer.Fit(self)
TFMtable.py:
import wx
import wx.grid
class TFMemployeeGridTable(wx.grid.Grid):
def __init__(self, parent, gridRows, rows):
wx.grid.Grid.__init__(self, parent)
self.CreateGrid(100,7)
self.SetColLabelValue(0, "ID")
self.SetColLabelValue(1, "employeeNumber")
self.SetColLabelValue(2, "RoomNum")
self.SetColLabelValue(3, "Last")
self.SetColLabelValue(4, "First")
self.SetColLabelValue(5, "Wage")
self.SetColLabelValue(6, "DateStated")
row = 0
for items in rows:
if row==0:
self.SetCellValue(row,0,str(items[0]))
self.SetCellBackgroundColour(row, 0, wx.LIGHT_GREY)
self.SetCellValue(row,1,str(items[1]))
self.SetCellBackgroundColour(row, 1, wx.LIGHT_GREY)
self.SetCellValue(row,2,str(items[2]))
self.SetCellBackgroundColour(row, 2, wx.LIGHT_GREY)
self.SetCellValue(row,3,str(items[3]))
self.SetCellBackgroundColour(row, 3, wx.LIGHT_GREY)
self.SetCellValue(row,4,str(items[4]))
self.SetCellBackgroundColour(row, 4, wx.LIGHT_GREY)
self.SetCellValue(row,5,str(items[5]))
self.SetCellBackgroundColour(row, 5, wx.LIGHT_GREY)
self.SetCellValue(row,6,str(items[6]))
self.SetCellBackgroundColour(row, 6, wx.LIGHT_GREY)
elif row%2==0:
self.SetCellValue(row,0,str(items[0]))
self.SetCellBackgroundColour(row, 0, wx.LIGHT_GREY)
self.SetCellValue(row,1,str(items[1]))
self.SetCellBackgroundColour(row, 1, wx.LIGHT_GREY)
self.SetCellValue(row,2,str(items[2]))
self.SetCellBackgroundColour(row, 2, wx.LIGHT_GREY)
self.SetCellValue(row,3,str(items[3]))
self.SetCellBackgroundColour(row, 3, wx.LIGHT_GREY)
self.SetCellValue(row,4,str(items[4]))
self.SetCellBackgroundColour(row, 4, wx.LIGHT_GREY)
self.SetCellValue(row,5,str(items[5]))
self.SetCellBackgroundColour(row, 5, wx.LIGHT_GREY)
self.SetCellValue(row,6,str(items[6]))
self.SetCellBackgroundColour(row, 6, wx.LIGHT_GREY)
else:
self.SetCellValue(row,0, ""+str(items[0]))
self.SetCellTextColour(row, 0, wx.BLACK)
self.SetCellBackgroundColour(row, 0, wx.WHITE)
self.SetCellValue(row,1, ""+str(items[1]))
self.SetCellTextColour(row, 1, wx.BLACK)
self.SetCellBackgroundColour(row, 1, wx.WHITE)
self.SetCellValue(row,2, ""+str(items[2]))
self.SetCellTextColour(row, 2, wx.BLACK)
self.SetCellBackgroundColour(row, 2, wx.WHITE)
self.SetCellValue(row,3, ""+str(items[3]))
self.SetCellTextColour(row, 3, wx.BLACK)
self.SetCellBackgroundColour(row, 3, wx.WHITE)
self.SetCellValue(row,4, ""+str(items[4]))
self.SetCellTextColour(row, 4, wx.BLACK)
self.SetCellBackgroundColour(row, 4, wx.WHITE)
self.SetCellValue(row,5, ""+str(items[5]))
self.SetCellTextColour(row, 5, wx.BLACK)
self.SetCellBackgroundColour(row, 5, wx.WHITE)
self.SetCellValue(row,6, ""+str(items[6]))
self.SetCellTextColour(row, 6, wx.BLACK)
self.SetCellBackgroundColour(row, 6, wx.WHITE)
row = row+1
self.SetSelectionMode(wx.grid.Grid.SelectRows)
self.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.CellLeftClick, self)
def CellLeftClick(self,e):
print("click on grid")