1. # -*- coding: cp936 -*-
  2. import wx
  3. import wx.grid
  4. import wx.lib.gridmovers as gridmovers
  5.  
  6. import pymssql
  7. connect=pymssql.connect(host='wxpython',user='sa',password='',database='THIS4_0807')
  8. cursor=connect.cursor()
  9. ascordesc=True
  10.  
  11. class LineupTable(wx.grid.PyGridTableBase):
  12. def __init__(self,data,fields):
  13. wx.grid.PyGridTableBase.__init__(self)
  14. self.data=data
  15. self.fields=fields
  16. ## self.dataTypes = [wx.grid.GRID_VALUE_STRING,
  17. ## wx.grid.GRID_VALUE_STRING,
  18. ## #gridlib.GRID_VALUE_CHOICE + ':only in a million years!,wish list,minor,normal,major,critical',
  19. ## #gridlib.GRID_VALUE_NUMBER + ':1,5',
  20. ## #gridlib.GRID_VALUE_CHOICE + ':all,MSW,GTK,other',
  21. ## #gridlib.GRID_VALUE_BOOL,
  22. ## #gridlib.GRID_VALUE_BOOL,
  23. ## #gridlib.GRID_VALUE_BOOL,
  24. ## wx.grid.GRID_VALUE_FLOAT + ':6,2',
  25. ## ]
  26.  
  27. #---Grid cell attributes
  28.  
  29. self.odd = wx.grid.GridCellAttr()
  30. self.odd.SetBackgroundColour("grey")
  31. self.odd.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD))
  32.  
  33. self.even = wx.grid.GridCellAttr()
  34. self.even.SetBackgroundColour("white")
  35. self.even.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD))
  36.  
  37. #---Mandatory constructors for grid
  38.  
  39. def GetNumberRows(self):
  40. # if len(self.data)<10:
  41. # rowcounts=10
  42. #else:
  43. #rowcounts=len(self.data)
  44. return len(self.data)
  45.  
  46. def GetNumberCols(self):
  47. return len(self.fields)
  48.  
  49. def GetColLabelValue(self, col):
  50. return self.fields[col]
  51.  
  52. def IsEmptyCell(self, row, col):
  53. if self.data[row][col] == "" or self.data[row][col] is None:
  54. return True
  55. else:
  56. return False
  57.  
  58. # def GetValue(self, row, col):
  59. # value = self.data[row][col]
  60. # if value is not None:
  61. # return value
  62. # else:
  63. # return ''
  64. #
  65. # def SetValue(self, row, col, value):
  66. # #print col
  67. # def innerSetValue(row, col, value):
  68. # try:
  69. # self.data[row][col] = value
  70. # except IndexError:
  71. # # add a new row
  72. # self.data.append([''] * self.GetNumberCols())
  73. # innerSetValue(row, col, value)
  74. #
  75. # # tell the grid we've added a row
  76. # msg = gridlib.GridTableMessage(self, # The table
  77. # gridlib.GRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it
  78. # 1 # how many
  79. # )
  80. #
  81. # self.GetView().ProcessTableMessage(msg)
  82. # innerSetValue(row, col, value)
  83.  
  84. def GetValue(self, row, col):
  85. #id = self.fields[col]
  86. return self.data[row][col]
  87.  
  88. def SetValue(self, row, col, value):
  89. #id = self.fields[col]
  90. self.data[row][col] = value
  91.  
  92. #--------------------------------------------------
  93. # Some optional methods
  94.  
  95. # Called when the grid needs to display column labels
  96. # def GetColLabelValue(self, col):
  97. # #id = self.fields[col]
  98. # return self.fields[col][0]
  99.  
  100. def GetAttr(self, row, col, kind):
  101. attr = [self.even, self.odd][row % 2]
  102. attr.IncRef()
  103. return attr
  104.  
  105. def SortColumn(self, col,ascordesc):
  106. """
  107. col -> sort the data based on the column indexed by col
  108. """
  109. name = self.fields[col]
  110. _data = []
  111.  
  112. for row in self.data:
  113. #print row
  114. #rowname, entry = row
  115.  
  116. _data.append((row[col], row))
  117.  
  118. _data.sort(reverse=ascordesc)
  119. self.data = []
  120.  
  121. for sortvalue, row in _data:
  122. self.data.append(row)
  123.  
  124. def AppendRow(self, row):#增加行
  125. #print 'append'
  126. entry = []
  127.  
  128. for name in self.fields:
  129. entry.append('A')
  130.  
  131. self.data.append(tuple(entry ))
  132. return True
  133.  
  134. def MoveColumn(self,frm,to):
  135. grid = self.GetView()
  136.  
  137. if grid:
  138. # Move the identifiers
  139. old = self.fields[frm]
  140. del self.fields[frm]
  141.  
  142. if to > frm:
  143. self.fields.insert(to-1,old)
  144. else:
  145. self.fields.insert(to,old)
  146.  
  147. print self.fields
  148.  
  149. # Notify the grid
  150. grid.BeginBatch()
  151.  
  152. msg = wx.grid.GridTableMessage(
  153. self, wx.grid.GRIDTABLE_NOTIFY_COLS_INSERTED, to, 1
  154. )
  155. grid.ProcessTableMessage(msg)
  156.  
  157. msg = wx.grid.GridTableMessage(
  158. self, wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, frm, 1
  159. )
  160. grid.ProcessTableMessage(msg)
  161.  
  162. grid.EndBatch()
  163.  
  164. # Move the row
  165. def MoveRow(self,frm,to):
  166. grid = self.GetView()
  167.  
  168. if grid:
  169. # Move the rowLabels and data rows
  170. oldLabel = self.rowLabels[frm]
  171. oldData = self.data[frm]
  172. del self.rowLabels[frm]
  173. del self.data[frm]
  174.  
  175. if to > frm:
  176. self.rowLabels.insert(to-1,oldLabel)
  177. self.data.insert(to-1,oldData)
  178. else:
  179. self.rowLabels.insert(to,oldLabel)
  180. self.data.insert(to,oldData)
  181.  
  182. # Notify the grid
  183. grid.BeginBatch()
  184.  
  185. msg = wx.grid.GridTableMessage(
  186. self, wx.grid.GRIDTABLE_NOTIFY_ROWS_INSERTED, to, 1
  187. )
  188. grid.ProcessTableMessage(msg)
  189.  
  190. msg = wx.grid.GridTableMessage(
  191. self, wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, frm, 1
  192. )
  193. grid.ProcessTableMessage(msg)
  194.  
  195. grid.EndBatch()
  196.  
  197. class DragableGrid(wx.grid.Grid):
  198. def __init__(self, parent):
  199. wx.grid.Grid.__init__(self, parent )
  200. cursor.execute('select top 2 blh,hzxm,qrrq from JK_YSQR_DK ')
  201. data = cursor.fetchall()
  202. fields = [cursor.description[i][0] for i in range(len(cursor.description))]
  203.  
  204. table = LineupTable(data,fields)
  205. self.SetTable(table, True)
  206. #table = LineupTable()
  207.  
  208. # The second parameter means that the grid is to take ownership of the
  209. # table and will destroy it when done. Otherwise you would need to keep
  210. # a reference to it and call it's Destroy method later.
  211. #self.SetTable(self.table, True)
  212.  
  213. # Enable Column moving
  214. gridmovers.GridColMover(self)
  215. self.Bind(gridmovers.EVT_GRID_COL_MOVE, self.OnColMove, self)
  216.  
  217. # Enable Row moving
  218. gridmovers.GridRowMover(self)
  219. self.Bind(gridmovers.EVT_GRID_ROW_MOVE, self.OnRowMove, self)
  220.  
  221. # Event method called when a column move needs to take place
  222. def OnColMove(self,evt):
  223. frm = evt.GetMoveColumn() # Column being moved
  224. to = evt.GetBeforeColumn() # Before which column to insert
  225. self.GetTable().MoveColumn(frm,to)
  226.  
  227. # Event method called when a row move needs to take place
  228. def OnRowMove(self,evt):
  229. frm = evt.GetMoveRow() # Row being moved
  230. to = evt.GetBeforeRow() # Before which row to insert
  231. self.GetTable().MoveRow(frm,to)
  232.  
  233. class MyFrame(wx.Frame):
  234. def __init__(self):
  235. wx.Frame.__init__(self, parent=None, id=-1, title='wx.grid.PyGridTableBase',size=(900,600))
  236.  
  237. #---Panel
  238.  
  239. #panel = wx.Panel(self, -1)
  240.  
  241. #---Buttons
  242.  
  243. self.btn_1hr = wx.Button(self, -1, "RUN", pos=(10, 10),size=(100,40))
  244. self.Bind(wx.EVT_BUTTON, self.OnClick, self.btn_1hr)
  245.  
  246. self.btn_2hr = wx.Button(self, -1, "ADD", pos=(10, 35),size=(100,40))
  247. self.Bind(wx.EVT_BUTTON, self.OnADD, self.btn_2hr)
  248. self.btn_3hr = wx.Button(self, -1, "DELETE", pos=(10, 60),size=(100,40))
  249.  
  250. self.btn_4hr = wx.Button(self, -1, "INSERT", pos=(10, 85),size=(100,40))
  251.  
  252. box = wx.BoxSizer(wx.VERTICAL)
  253. box.Add(self.btn_1hr, 1, wx.EXPAND,5)
  254. box.Add(self.btn_2hr, 1, wx.EXPAND,5)
  255. box.Add(self.btn_3hr, 1, wx.EXPAND,5)
  256. box.Add(self.btn_4hr, 1, wx.EXPAND,5)
  257.  
  258. #---Grid
  259.  
  260. self.grid =DragableGrid(self)#, pos=(140, 0), size=(900,400))
  261. self.grid.SetRowLabelSize(40)#设置行标签的宽度
  262. self.Bind(wx.grid.EVT_GRID_LABEL_RIGHT_CLICK, self.OnLabelRightClick)
  263. #self.Bind(wx.grid.EVT_GRID_RANGE_SELECT, self.OnRangeSelect)
  264. box1 = wx.BoxSizer(wx.VERTICAL)
  265. box1.Add(self.grid, 1, wx.GROW|wx.ALL)
  266. cursor.execute('select top 2 blh,hzxm,qrrq from JK_YSQR_DK ')
  267. data = cursor.fetchall()
  268. fields = [cursor.description[i][0] for i in range(len(cursor.description))]
  269.  
  270. self.table = LineupTable(data,fields)
  271. self.grid.SetTable(self.table, True)
  272.  
  273. #---Grid properties
  274.  
  275. self.grid.EnableEditing(True)#是否可以编辑
  276. self.grid.SetDefaultCellAlignment(wx.ALIGN_LEFT, wx.ALIGN_RIGHT)#设置CELL的文本对齐方式
  277. self.grid.SetSelectionBackground('red')
  278. self.grid.EnableDragColSize(enable=True)#控制列宽是否可以拉动
  279. self.grid.EnableDragRowSize(enable=True)#控制行高是否可以拉动
  280. self.grid.SetLabelBackgroundColour((100, 200, 150))
  281. self.grid.SetLabelTextColour((255, 255, 255))
  282.  
  283. #---Column Sizes
  284. self.grid.AdjustScrollbars()
  285. self.grid.Refresh()
  286.  
  287. Hbox = wx.BoxSizer(wx.HORIZONTAL)
  288. Hbox.Add(box, 0, wx.EXPAND)
  289. Hbox.Add(box1, 1, wx.EXPAND)
  290. #Vbox = wx.BoxSizer(wx.VERTICAL)
  291. #Vbox.Add(Hbox,0,wx.ALL|wx.EXPAND)
  292.  
  293. self.SetSizer(Hbox)
  294. #self.Fit()
  295. #self.grid.AutoSize()
  296.  
  297. #---Use below if want to size individual columns (index, size)
  298. #---Also have SetRowSize
  299. #grid.SetColSize(0, 150)
  300.  
  301. def Reset(self):
  302. """reset the view based on the data in the table. Call
  303. this when rows are added or destroyed"""
  304. self.table.ResetView(self)
  305.  
  306. def OnLabelRightClick(self, event):
  307. global ascordesc,row, col
  308.  
  309. #self.SetStatusText('You Have Right-Clicked On Label "%s"!' % event.GetString())
  310. row, col = event.GetRow(), event.GetCol()
  311. #print row, col
  312. self.table.SortColumn(col,ascordesc)
  313. if ascordesc:
  314. ascordesc=False
  315. else:
  316. ascordesc=True
  317.  
  318. self.grid.Refresh()
  319.  
  320. def OnADD(self,event):
  321.  
  322. #self.table.AppendRow(row)
  323. #print (self.grid.SelectedRows)
  324. self.table.AppendRow(row)
  325. #self.grid.SetTable(self.table, True)
  326. self.grid.ForceRefresh()
  327.  
  328. def OnClick(self, event):
  329.  
  330. cursor.execute('select top 5 id as "编码",name as "名称" ,cast(memo as numeric(12,2)) as "单价" from YY_SFDXMK where memo>0')
  331. data1 = cursor.fetchall()
  332. fields1 = [cursor.description[i][0] for i in range(len(cursor.description))]
  333.  
  334. self.table = LineupTable(data1,fields1)
  335. self.grid.SetTable(self.table, True)
  336. self.grid.EnableDragColSize(enable=True)
  337. self.grid.EnableDragRowSize(enable=True)
  338. #self.grid.AutoSize()
  339. self.grid.AdjustScrollbars()
  340. #self.grid.ForceRefresh()
  341. self.grid.Refresh()
  342.  
  343. if __name__ == "__main__":
  344. app = wx.PySimpleApp()
  345. frame = MyFrame()
  346. frame.Show()
  347. app.MainLoop()

wx.grid.Grid的更多相关文章

  1. wxPython控件学习之wx.grid.Grid 表格控件

    wxPython控件学习之wx.grid.Grid (包括对GridCellEditor和GridCelRender的扩展,以支持更多的grid cell 样式, 以GridCellColorEdit ...

  2. Python Tkinter模块 Grid(grid)布局管理器参数详解

    在使用Tkinter模块编写图像界面时,经常用到pack()和grid()进行布局管理,pack()参数较少,使用方便,是最简单的布局,但是当控件数量较多时,可能需要使用grid()进行布局(不要在同 ...

  3. wx.grid

    wxPython控件学习之wx.grid.Grid (包括对GridCellEditor和GridCelRender的扩展,以支持更多的grid cell 样式, 以GridCellColorEdit ...

  4. wx.grid 简单例子

    import wx, wx.grid class GridData(wx.grid.PyGridTableBase): _cols = "a b c".split() _data ...

  5. 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 ...

  6. wxpython grid

    构建Grid方法,效果如下: 其它构建grid方法和grid的使用见:还可以见下载资源中的wxpython教程第5章的 gridGeneric.py gridModel.py gridNoModel. ...

  7. Grid 布局管理器

    Grid 布局管理器: Grid布局类wx.GridSizer,Grid布局以网格形式对子窗口或控件进行摆放,容器被分成大小相等的矩形,一个矩形中放置一个子窗口或控件. wx.GridSizer构造方 ...

  8. WPF中Grid实现网格,表格样式通用类

    /// <summary> /// 给Grid添加边框线 /// </summary> /// <param name="grid"></ ...

  9. wpf 列表、菜单 收起与展开,通过Grid DoubleAnimation或者Expander实现

    菜单收缩有很多种方法具体如何实现还是看个人想法: 第一种通过后台控制收起与展开: 效果图: 代码 : <Grid> <Grid.ColumnDefinitions> <C ...

随机推荐

  1. scrapy的 安装 及 流程 转

    安装 linux 和 mac 直接  pip install scrapy 就行 windows 安装步骤         a. pip3 install wheel       b. 下载twist ...

  2. EasyUI学习笔记(一)EasyUI入门

    一.EasyUI下载 EasyUI官方下载地址:http://www.jeasyui.com/download/index.php,目前最新的版本是:jquery-easyui-1.7.2 解压后得到 ...

  3. 去掉小程序button元素的边框

    button::after {     display:none }

  4. SGU - 507 启发式合并维护平衡树信息

    题意:给定一颗树,每个叶子节点\(u\)都有权值\(val[u]\),求每个非叶子节点子树的最小叶子距离,若该子树只有一个叶子节点,输出INF 貌似本来是一道树分治(并不会)的题目,然而可以利用平衡树 ...

  5. ACM自己之前寒假的基础总结

    1.const double pi = acos(-1.0); acos:反余弦函数,需要#include<math.h>函数库,acos(-1.0)的意思就是求π的值 2.算法竞赛中,不 ...

  6. SSH 项目建立过程

    1. 加入 Spring 1). 加入 jar 包 2). 配置 web.xml 文件 <context-param> <param-name>contextConfigLoc ...

  7. 什么是javascript的中间件?

    第一次写博客,有点想在博客园试水的感觉,也分享下觉得有用的东西(源码自己写的) 什么是javascript中间件呢?函数middle就是用来构建中间件的,我用例子说明下 下面我定义了一个函数use,在 ...

  8. 【研究】Struts2-048漏洞

    1.1 漏洞背景 2017年7月7日,Apache Struts发布最新的安全公告,Apache Struts2-strus1-plugin插件存在远程代码执行的高危漏洞,漏洞编号为CVE-2017- ...

  9. 【记录】BurpSuite之Grep-Extract

    借助一次sql注入来说明Grep-Extract的作用 要报出当前数据库中所有表名,这里可以有多种方法,我借助limit语句,以此来说明Grep-Extract的用法.

  10. java将文本写入本地硬盘

    注意:首先要在E盘创建qaz.txt文本文件.然后执行代码写入. public static void main(String[] args) { SecurityCodeUtils scu = ne ...