wx.grid.Grid
- # -*- coding: cp936 -*-
- import wx
- import wx.grid
- import wx.lib.gridmovers as gridmovers
- import pymssql
- connect=pymssql.connect(host='wxpython',user='sa',password='',database='THIS4_0807')
- cursor=connect.cursor()
- ascordesc=True
- class LineupTable(wx.grid.PyGridTableBase):
- def __init__(self,data,fields):
- wx.grid.PyGridTableBase.__init__(self)
- self.data=data
- self.fields=fields
- ## self.dataTypes = [wx.grid.GRID_VALUE_STRING,
- ## wx.grid.GRID_VALUE_STRING,
- ## #gridlib.GRID_VALUE_CHOICE + ':only in a million years!,wish list,minor,normal,major,critical',
- ## #gridlib.GRID_VALUE_NUMBER + ':1,5',
- ## #gridlib.GRID_VALUE_CHOICE + ':all,MSW,GTK,other',
- ## #gridlib.GRID_VALUE_BOOL,
- ## #gridlib.GRID_VALUE_BOOL,
- ## #gridlib.GRID_VALUE_BOOL,
- ## wx.grid.GRID_VALUE_FLOAT + ':6,2',
- ## ]
- #---Grid cell attributes
- self.odd = wx.grid.GridCellAttr()
- self.odd.SetBackgroundColour("grey")
- self.odd.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD))
- self.even = wx.grid.GridCellAttr()
- self.even.SetBackgroundColour("white")
- self.even.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD))
- #---Mandatory constructors for grid
- def GetNumberRows(self):
- # if len(self.data)<10:
- # rowcounts=10
- #else:
- #rowcounts=len(self.data)
- return len(self.data)
- def GetNumberCols(self):
- return len(self.fields)
- def GetColLabelValue(self, col):
- return self.fields[col]
- def IsEmptyCell(self, row, col):
- if self.data[row][col] == "" or self.data[row][col] is None:
- return True
- else:
- return False
- # def GetValue(self, row, col):
- # value = self.data[row][col]
- # if value is not None:
- # return value
- # else:
- # return ''
- #
- # def SetValue(self, row, col, value):
- # #print col
- # def innerSetValue(row, col, value):
- # try:
- # self.data[row][col] = value
- # except IndexError:
- # # add a new row
- # self.data.append([''] * self.GetNumberCols())
- # innerSetValue(row, col, value)
- #
- # # tell the grid we've added a row
- # msg = gridlib.GridTableMessage(self, # The table
- # gridlib.GRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it
- # 1 # how many
- # )
- #
- # self.GetView().ProcessTableMessage(msg)
- # innerSetValue(row, col, value)
- def GetValue(self, row, col):
- #id = self.fields[col]
- return self.data[row][col]
- def SetValue(self, row, col, value):
- #id = self.fields[col]
- self.data[row][col] = value
- #--------------------------------------------------
- # Some optional methods
- # Called when the grid needs to display column labels
- # def GetColLabelValue(self, col):
- # #id = self.fields[col]
- # return self.fields[col][0]
- def GetAttr(self, row, col, kind):
- attr = [self.even, self.odd][row % 2]
- attr.IncRef()
- return attr
- def SortColumn(self, col,ascordesc):
- """
- col -> sort the data based on the column indexed by col
- """
- name = self.fields[col]
- _data = []
- for row in self.data:
- #print row
- #rowname, entry = row
- _data.append((row[col], row))
- _data.sort(reverse=ascordesc)
- self.data = []
- for sortvalue, row in _data:
- self.data.append(row)
- def AppendRow(self, row):#增加行
- #print 'append'
- entry = []
- for name in self.fields:
- entry.append('A')
- self.data.append(tuple(entry ))
- return True
- def MoveColumn(self,frm,to):
- grid = self.GetView()
- if grid:
- # Move the identifiers
- old = self.fields[frm]
- del self.fields[frm]
- if to > frm:
- self.fields.insert(to-1,old)
- else:
- self.fields.insert(to,old)
- print self.fields
- # Notify the grid
- grid.BeginBatch()
- msg = wx.grid.GridTableMessage(
- self, wx.grid.GRIDTABLE_NOTIFY_COLS_INSERTED, to, 1
- )
- grid.ProcessTableMessage(msg)
- msg = wx.grid.GridTableMessage(
- self, wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, frm, 1
- )
- grid.ProcessTableMessage(msg)
- grid.EndBatch()
- # Move the row
- def MoveRow(self,frm,to):
- grid = self.GetView()
- if grid:
- # Move the rowLabels and data rows
- oldLabel = self.rowLabels[frm]
- oldData = self.data[frm]
- del self.rowLabels[frm]
- del self.data[frm]
- if to > frm:
- self.rowLabels.insert(to-1,oldLabel)
- self.data.insert(to-1,oldData)
- else:
- self.rowLabels.insert(to,oldLabel)
- self.data.insert(to,oldData)
- # Notify the grid
- grid.BeginBatch()
- msg = wx.grid.GridTableMessage(
- self, wx.grid.GRIDTABLE_NOTIFY_ROWS_INSERTED, to, 1
- )
- grid.ProcessTableMessage(msg)
- msg = wx.grid.GridTableMessage(
- self, wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, frm, 1
- )
- grid.ProcessTableMessage(msg)
- grid.EndBatch()
- class DragableGrid(wx.grid.Grid):
- def __init__(self, parent):
- wx.grid.Grid.__init__(self, parent )
- cursor.execute('select top 2 blh,hzxm,qrrq from JK_YSQR_DK ')
- data = cursor.fetchall()
- fields = [cursor.description[i][0] for i in range(len(cursor.description))]
- table = LineupTable(data,fields)
- self.SetTable(table, True)
- #table = LineupTable()
- # The second parameter means that the grid is to take ownership of the
- # table and will destroy it when done. Otherwise you would need to keep
- # a reference to it and call it's Destroy method later.
- #self.SetTable(self.table, True)
- # Enable Column moving
- gridmovers.GridColMover(self)
- self.Bind(gridmovers.EVT_GRID_COL_MOVE, self.OnColMove, self)
- # Enable Row moving
- gridmovers.GridRowMover(self)
- self.Bind(gridmovers.EVT_GRID_ROW_MOVE, self.OnRowMove, self)
- # Event method called when a column move needs to take place
- def OnColMove(self,evt):
- frm = evt.GetMoveColumn() # Column being moved
- to = evt.GetBeforeColumn() # Before which column to insert
- self.GetTable().MoveColumn(frm,to)
- # Event method called when a row move needs to take place
- def OnRowMove(self,evt):
- frm = evt.GetMoveRow() # Row being moved
- to = evt.GetBeforeRow() # Before which row to insert
- self.GetTable().MoveRow(frm,to)
- class MyFrame(wx.Frame):
- def __init__(self):
- wx.Frame.__init__(self, parent=None, id=-1, title='wx.grid.PyGridTableBase',size=(900,600))
- #---Panel
- #panel = wx.Panel(self, -1)
- #---Buttons
- self.btn_1hr = wx.Button(self, -1, "RUN", pos=(10, 10),size=(100,40))
- self.Bind(wx.EVT_BUTTON, self.OnClick, self.btn_1hr)
- self.btn_2hr = wx.Button(self, -1, "ADD", pos=(10, 35),size=(100,40))
- self.Bind(wx.EVT_BUTTON, self.OnADD, self.btn_2hr)
- self.btn_3hr = wx.Button(self, -1, "DELETE", pos=(10, 60),size=(100,40))
- self.btn_4hr = wx.Button(self, -1, "INSERT", pos=(10, 85),size=(100,40))
- box = wx.BoxSizer(wx.VERTICAL)
- box.Add(self.btn_1hr, 1, wx.EXPAND,5)
- box.Add(self.btn_2hr, 1, wx.EXPAND,5)
- box.Add(self.btn_3hr, 1, wx.EXPAND,5)
- box.Add(self.btn_4hr, 1, wx.EXPAND,5)
- #---Grid
- self.grid =DragableGrid(self)#, pos=(140, 0), size=(900,400))
- self.grid.SetRowLabelSize(40)#设置行标签的宽度
- self.Bind(wx.grid.EVT_GRID_LABEL_RIGHT_CLICK, self.OnLabelRightClick)
- #self.Bind(wx.grid.EVT_GRID_RANGE_SELECT, self.OnRangeSelect)
- box1 = wx.BoxSizer(wx.VERTICAL)
- box1.Add(self.grid, 1, wx.GROW|wx.ALL)
- cursor.execute('select top 2 blh,hzxm,qrrq from JK_YSQR_DK ')
- data = cursor.fetchall()
- fields = [cursor.description[i][0] for i in range(len(cursor.description))]
- self.table = LineupTable(data,fields)
- self.grid.SetTable(self.table, True)
- #---Grid properties
- self.grid.EnableEditing(True)#是否可以编辑
- self.grid.SetDefaultCellAlignment(wx.ALIGN_LEFT, wx.ALIGN_RIGHT)#设置CELL的文本对齐方式
- self.grid.SetSelectionBackground('red')
- self.grid.EnableDragColSize(enable=True)#控制列宽是否可以拉动
- self.grid.EnableDragRowSize(enable=True)#控制行高是否可以拉动
- self.grid.SetLabelBackgroundColour((100, 200, 150))
- self.grid.SetLabelTextColour((255, 255, 255))
- #---Column Sizes
- self.grid.AdjustScrollbars()
- self.grid.Refresh()
- Hbox = wx.BoxSizer(wx.HORIZONTAL)
- Hbox.Add(box, 0, wx.EXPAND)
- Hbox.Add(box1, 1, wx.EXPAND)
- #Vbox = wx.BoxSizer(wx.VERTICAL)
- #Vbox.Add(Hbox,0,wx.ALL|wx.EXPAND)
- self.SetSizer(Hbox)
- #self.Fit()
- #self.grid.AutoSize()
- #---Use below if want to size individual columns (index, size)
- #---Also have SetRowSize
- #grid.SetColSize(0, 150)
- def Reset(self):
- """reset the view based on the data in the table. Call
- this when rows are added or destroyed"""
- self.table.ResetView(self)
- def OnLabelRightClick(self, event):
- global ascordesc,row, col
- #self.SetStatusText('You Have Right-Clicked On Label "%s"!' % event.GetString())
- row, col = event.GetRow(), event.GetCol()
- #print row, col
- self.table.SortColumn(col,ascordesc)
- if ascordesc:
- ascordesc=False
- else:
- ascordesc=True
- self.grid.Refresh()
- def OnADD(self,event):
- #self.table.AppendRow(row)
- #print (self.grid.SelectedRows)
- self.table.AppendRow(row)
- #self.grid.SetTable(self.table, True)
- self.grid.ForceRefresh()
- def OnClick(self, event):
- cursor.execute('select top 5 id as "编码",name as "名称" ,cast(memo as numeric(12,2)) as "单价" from YY_SFDXMK where memo>0')
- data1 = cursor.fetchall()
- fields1 = [cursor.description[i][0] for i in range(len(cursor.description))]
- self.table = LineupTable(data1,fields1)
- self.grid.SetTable(self.table, True)
- self.grid.EnableDragColSize(enable=True)
- self.grid.EnableDragRowSize(enable=True)
- #self.grid.AutoSize()
- self.grid.AdjustScrollbars()
- #self.grid.ForceRefresh()
- self.grid.Refresh()
- if __name__ == "__main__":
- app = wx.PySimpleApp()
- frame = MyFrame()
- frame.Show()
- app.MainLoop()
wx.grid.Grid的更多相关文章
- wxPython控件学习之wx.grid.Grid 表格控件
wxPython控件学习之wx.grid.Grid (包括对GridCellEditor和GridCelRender的扩展,以支持更多的grid cell 样式, 以GridCellColorEdit ...
- Python Tkinter模块 Grid(grid)布局管理器参数详解
在使用Tkinter模块编写图像界面时,经常用到pack()和grid()进行布局管理,pack()参数较少,使用方便,是最简单的布局,但是当控件数量较多时,可能需要使用grid()进行布局(不要在同 ...
- wx.grid
wxPython控件学习之wx.grid.Grid (包括对GridCellEditor和GridCelRender的扩展,以支持更多的grid cell 样式, 以GridCellColorEdit ...
- wx.grid 简单例子
import wx, wx.grid class GridData(wx.grid.PyGridTableBase): _cols = "a b c".split() _data ...
- 46-wxpython 4 使用 grid 展示表格
转载:https://blog.csdn.net/soslinken/article/details/79024938#%E4%BD%BF%E7%94%A8%E6%A0%B7%E4%BE%8B wxp ...
- wxpython grid
构建Grid方法,效果如下: 其它构建grid方法和grid的使用见:还可以见下载资源中的wxpython教程第5章的 gridGeneric.py gridModel.py gridNoModel. ...
- Grid 布局管理器
Grid 布局管理器: Grid布局类wx.GridSizer,Grid布局以网格形式对子窗口或控件进行摆放,容器被分成大小相等的矩形,一个矩形中放置一个子窗口或控件. wx.GridSizer构造方 ...
- WPF中Grid实现网格,表格样式通用类
/// <summary> /// 给Grid添加边框线 /// </summary> /// <param name="grid"></ ...
- wpf 列表、菜单 收起与展开,通过Grid DoubleAnimation或者Expander实现
菜单收缩有很多种方法具体如何实现还是看个人想法: 第一种通过后台控制收起与展开: 效果图: 代码 : <Grid> <Grid.ColumnDefinitions> <C ...
随机推荐
- scrapy的 安装 及 流程 转
安装 linux 和 mac 直接 pip install scrapy 就行 windows 安装步骤 a. pip3 install wheel b. 下载twist ...
- EasyUI学习笔记(一)EasyUI入门
一.EasyUI下载 EasyUI官方下载地址:http://www.jeasyui.com/download/index.php,目前最新的版本是:jquery-easyui-1.7.2 解压后得到 ...
- 去掉小程序button元素的边框
button::after { display:none }
- SGU - 507 启发式合并维护平衡树信息
题意:给定一颗树,每个叶子节点\(u\)都有权值\(val[u]\),求每个非叶子节点子树的最小叶子距离,若该子树只有一个叶子节点,输出INF 貌似本来是一道树分治(并不会)的题目,然而可以利用平衡树 ...
- ACM自己之前寒假的基础总结
1.const double pi = acos(-1.0); acos:反余弦函数,需要#include<math.h>函数库,acos(-1.0)的意思就是求π的值 2.算法竞赛中,不 ...
- SSH 项目建立过程
1. 加入 Spring 1). 加入 jar 包 2). 配置 web.xml 文件 <context-param> <param-name>contextConfigLoc ...
- 什么是javascript的中间件?
第一次写博客,有点想在博客园试水的感觉,也分享下觉得有用的东西(源码自己写的) 什么是javascript中间件呢?函数middle就是用来构建中间件的,我用例子说明下 下面我定义了一个函数use,在 ...
- 【研究】Struts2-048漏洞
1.1 漏洞背景 2017年7月7日,Apache Struts发布最新的安全公告,Apache Struts2-strus1-plugin插件存在远程代码执行的高危漏洞,漏洞编号为CVE-2017- ...
- 【记录】BurpSuite之Grep-Extract
借助一次sql注入来说明Grep-Extract的作用 要报出当前数据库中所有表名,这里可以有多种方法,我借助limit语句,以此来说明Grep-Extract的用法.
- java将文本写入本地硬盘
注意:首先要在E盘创建qaz.txt文本文件.然后执行代码写入. public static void main(String[] args) { SecurityCodeUtils scu = ne ...