0

我正在尝试显示一个网格以及一些按钮,用于使用 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")
4

1 回答 1

2

您可以使用 wxPython 小部件检查工具自我诊断大多数视觉布局问题,您可以在此处阅读:

它将帮助向您展示小部件如何重叠、小部件的父级是什么、小部件的大小等。我还注意到您将self.gridPnl 的ID 设置为 1。这很糟糕。小于 100 的 ID 通常由 wxPython 保留,这就是为什么您通常会在在线示例应用程序中看到魔法 -1 或 wx.ID_ANY。-1 和 wx.ID_ANY 告诉 wxPython 动态创建一个不会与当前使用的任何其他 ID 冲突的 ID。

最后,这是一个超级简单的例子,展示了如何创建一个网格,下面有几个按钮:

import wx
import wx.grid as gridlib

########################################################################
class MyForm(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, parent=None, title="A Simple Grid")
        panel = wx.Panel(self)

        myGrid = gridlib.Grid(panel)
        myGrid.CreateGrid(12, 8)

        btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
        btn_one = wx.Button(panel, label="One")
        btn_sizer.Add(btn_one, 0, wx.CENTER|wx.ALL, 5)

        btn_two = wx.Button(panel, label="Two")
        btn_sizer.Add(btn_two, 0, wx.CENTER|wx.ALL, 5)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(myGrid, 1, wx.EXPAND)
        sizer.Add(btn_sizer)
        panel.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()
于 2014-08-18T18:32:45.947 回答