首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wxPython显示网格在BoxSizer中的应用

wxPython显示网格在BoxSizer中的应用
EN

Stack Overflow用户
提问于 2014-08-18 17:32:48
回答 1查看 1.6K关注 0票数 0

我正在尝试显示一个网格和一些按钮,以便在wx.Panel上使用wx.Boxsizer导航我的表。如果我首先将按钮添加到BoxSizer中,其中按钮位于垂直布局的网格顶部,我就可以得到网格和按钮的正确显示。如果我在垂直布局中网格位于按钮顶部的位置切换顺序,按钮将不会显示,网格将占据整个面板。

作品:

代码语言:javascript
复制
    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)

不起作用:

代码语言:javascript
复制
    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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-18 18:32:45

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

  • http://wiki.wxpython.org/Widget%20Inspection%20Tool

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

最后,下面是一个非常简单的示例,展示了如何创建一个网格,并在其下面有几个按钮:

代码语言:javascript
复制
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()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25368901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档