wxPython中单元格示例

来源:互联网 发布:英菲克安装软件密码 编辑:程序博客网 时间:2024/06/08 18:41

主要是一些资料的汇总:

#!/usr/bin/python
#-*- coding:utf8 -*-


import wx
import wx.grid as grid

class EventTable(grid.PyGridTableBase):
    def __init__(self):
        grid.PyGridTableBase.__init__(self)
        self.odd = grid.GridCellAttr()
        self.odd.SetBackgroundColour("yellow")
        self.even = grid.GridCellAttr()

    def GetAttr(self, row, col, kind):
        attr = [self.even, self.odd][row % 2]
        attr.IncRef()
        return attr

    def GetNumberRows(self):
        return 8

    def GetNumberCols(self):
        return 8

    def GetValue(self, row, col):
        return str(col)

    def SetValue(self, row, col, value):
        print (row, col, value)

class Frame(wx.Frame):
    def __init__(self, root):
        wx.Frame.__init__(self, root, -1, u"表格", size=(450,250))
        gridTest = grid.Grid(self)
        table = EventTable()
        gridTest.SetTable(table, True)

class MyApp(wx.App):
    def OnInit(self):
        self.frame = Frame(None)
        self.frame.Show()
        return True

app = MyApp()
app.MainLoop()


示例2:

#!/usr/bin/python
#-*- coding:utf8 -*-

import wx
import wx.grid

class GridFrame(wx.Frame):
    def __init__(self, root):
        wx.Frame.__init__(self, root, -1, size=(450,450))
        row = [u"第一行", u"第二行", u"第三行", u"第四行"]
        col = [u"第一列", u"第二列", u"第三列", u"第四列"]

        grid = wx.grid.Grid(self)
        grid.CreateGrid(len(row), len(col))        

        for r in range(len(row)):
            grid.SetRowLabelValue(r, row[r])
            grid.SetColLabelValue(r, col[r])
            for c in range(len(col)):
                grid.SetCellValue(r, c, str(c))

class MyApp(wx.App):
    def OnInit(self):
        self.frame = GridFrame(None)
        self.frame.Show()
        return True


app = MyApp()
app.MainLoop()


示例3:

#!/usr/bin/python
#-*- coding:utf8 -*-

import wx
import wx.grid as grid
import pprint

class EventFrame(grid.Grid):
    def __init__(self, root):
        grid.Grid.__init__(self, root, -1)
        self.CreateGrid(5, 5)
    
        #鼠标左击
        self.Bind(grid.EVT_GRID_CELL_LEFT_CLICK, self.OnCellLeftClick)
        #鼠标右击
        self.Bind(grid.EVT_GRID_CELL_RIGHT_CLICK, self.OnCellRightClick)
        #选择单元格
        self.Bind(grid.EVT_GRID_SELECT_CELL, self.OnSelectCell)
        #选择多个单元格
        self.Bind(grid.EVT_GRID_RANGE_SELECT, self.OnRangeSelect)
        #单元格内容改变时
        self.Bind(grid.EVT_GRID_CELL_CHANGE, self.OnCellChange)
        #编辑框 发生改变时
        self.Bind(grid.EVT_GRID_EDITOR_SHOWN, self.OnEditorShown)
        self.Bind(grid.EVT_GRID_EDITOR_HIDDEN, self.OnEditorHidden)
        self.Bind(grid.EVT_GRID_EDITOR_CREATED, self.OnEditorCreated)
        #键盘事件
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

    def OnCellLeftClick(self, evt):
        print "(%d, %d)"%(evt.GetRow(), evt.GetCol())
        evt.Skip()

    def OnCellRightClick(self, evt):
        wx.MessageBox("%s"%(evt.GetPosition()), u"提示")
        evt.Skip()

    def OnRangeSelect(self, evt):
        if evt.Selecting():
            print "(%s, %s)"%(evt.GetTopLeftCoords(), evt.GetBottomRightCoords())
        evt.Skip()

    def OnCellChange(self, evt):
        value = self.GetCellValue(evt.GetRow(), evt.GetCol())
        print value

    def OnSelectCell(self, evt):
        self.SetCellBackgroundColour(evt.GetRow(), evt.GetCol(), wx.RED)
        evt.Skip()

    def OnEditorShown(self, evt):
        print "show"
        evt.Skip()

    def OnEditorHidden(self, evt):
        print "hidden"
        evt.Skip()

    def OnEditorCreated(self, evt):
        print evt.GetControl()
        evt.Skip()

    def OnKeyDown(self, evt):
        value = evt.GetPosition()
        pp = pprint.PrettyPrinter()
        pp.pprint(value)
        print "KeyCode:" + str(evt.GetKeyCode())
        evt.Skip()

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, u"表格的事件", size=(500,180))
        self.grid = EventFrame(self)


class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame()
        self.frame.Show()
        return True

app = MyApp()
app.MainLoop()

示例4:

#!/usr/bin/python
#-*- coding:utf8 -*-

import wx
import wx.grid as grid

class SimpleGrid(grid.Grid):
    def __init__(self, root):
        grid.Grid.__init__(self, root, -1)
        self.CreateGrid(8, 4)
        self.EnableEditing(True)
    
        self.SetColLabelValue(0, u"第一列")
        self.SetColLabelAlignment(wx.ALIGN_LEFT, wx.ALIGN_BOTTOM)
        self.SetRowLabelValue(0, u"第一行")
        self.SetRowLabelAlignment(wx.ALIGN_RIGHT, wx.ALIGN_BOTTOM)
        
        self.SetCellValue(0, 0, "(0,0)")
        self.SetCellTextColour(0, 0, wx.RED)
        self.SetCellFont(0, 0, wx.Font(10, wx.ROMAN, wx.ITALIC, wx.NORMAL))
        
        self.SetCellValue(1, 0, "Read Only")
        self.SetCellBackgroundColour(1, 0, wx.RED)
    
        self.SetCellEditor(2, 0, grid.GridCellNumberEditor(1, 10))
        self.SetCellValue(2, 0, "1")
        self.SetCellEditor(2, 1, grid.GridCellFloatEditor(0, 1))
        self.SetCellValue(2, 1, "10.19")
        attr = grid.GridCellAttr()                          
        attr.SetTextColour(wx.BLUE)
        self.SetColAttr(1, attr)                                            
        self.SetCellAlignment(5, 1, wx.ALIGN_CENTRE, wx.ALIGN_BOTTOM)       
        self.SetCellValue(5, 1, u"跨行、列的单元格")
        self.SetCellSize(5, 1, 2, 2)                                        
        self.SetCellEditor(6, 0, grid.GridCellChoiceEditor(["one", "two", "three"]))
        self.SetCellValue(6, 0, "one")
        self.SetCellEditor(7, 0, grid.GridCellBoolEditor())
        self.SetCellValue(7, 0, "True")


class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, u"单元格的设置", size=(450,250))
        self.grid = SimpleGrid(self)


class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame()
        self.frame.Show()
        return True

app = MyApp()
app.MainLoop()


结合这些,读者应该可以写出一个比较高级的单元格程序了

0 0
原创粉丝点击