wxPython在BoxSiz中显示网格

2024-09-30 20:36:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试显示一个网格以及一些按钮,以便在wx.面板使用wx.Boxsizer公司. 如果我先将按钮添加到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)

不起作用:

^{pr2}$

我所有的代码: 表格视图.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")

Tags: selfadditemsgreyrowlightexpandwx
1条回答
网友
1楼 · 发布于 2024-09-30 20:36:38

您可以使用wxPython Widget检查工具自诊断大多数视觉布局问题,您可以在这里阅读:

这将有助于向您展示窗口小部件如何重叠,小部件的父级是什么,小部件的大小等等。我还注意到您正在设置self.gridPnl的ID设置为1。这很糟糕。小于100的id通常由wxPython保留,这就是为什么您通常会看到magic-1或wx.ID\u任何在示例apps online中。-1和wx.ID\u任何告诉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()

相关问题 更多 >