14.2.2 how to deal with the header of rows and columns in a grid?
In a wxpython grid control, each row and each column have their own labels. By default, the row label is a number, starting from 1. The column label is a letter, starting with. Wxpython provides some methods to change these labels. Figure 14.3 shows a grid with a header label.
Fig 14.3
Example 14.3 showsCode. The mesh is initialized with creategrid.
Example 14.3 a non-pattern grid with custom tags
Import WX
Import wx. Grid
Class testframe (wx. Frame ):
Rowlabels = ["uno", "dos", "tres", "Quatro", "cinco"]
Collabels = ["Homer", "Marge", "Bart", "Lisa", "Maggie"]
Def _ init _ (Self ):
Wx. Frame. _ init _ (self, none, Title = "Grid headers ",
Size = (500,200 ))
Grid = wx. Grid. Grid (Self)
Grid. creategrid (5, 5)
For row in range (5 ):
#1 start
Grid. setrowlabelvalue (row, self. rowlabels [row])
Grid. setcollabelvalue (row, self. collabels [row])
#1 end
For Col in range (5 ):
Grid. setcellvalue (row, Col,
"(% S, % s)" % (self. rowlabels [row], self. collabels [col])
APP = wx. pysimpleapp ()
Frame = testframe ()
Frame. Show ()
App. mainloop ()
Just like adding and deleting rows, changing tags varies depending on the grid type. For a grid created using creategrid (), use the setcollabelvalue (COL, value) and setrowlabelvalue (row, value) Methods to set the tag value, as shown in #1. The Col and row parameters are column and row indexes, and the value is the string to be displayed in the tag. To get a label for a row or column, use the getcollabelvalue (COL) and getrowlabelvalue (ROW) methods.
To use a grid control of an external grid table, you can use the getcollabelvalue (COL) and getrowlabelvalue (ROW) Methods of the grid table to achieve the same effect. To eliminate confusion, the grid control internally calls these methods when it needs to display tags and the grid has an associated table. Since the return value is dynamically determined by the code you write in the override method, you do not need to overwrite or call the Set * method here. However, the Set * method still exists -- setcollabelvalue (COL, value) and setrowlabelvalue (row, value) -- but you rarely use it unless you want users to change the potential data. Generally, you do not need the Set * method. Example 14.4 shows how to change tags in a grid table -- this example produces the same output as the previous example.
Example 14.4 use a grid with custom tags
Import WX
Import wx. Grid
Class testtable (wx. Grid. pygridtablebase ):
Def _ init _ (Self ):
Wx. Grid. pygridtablebase. _ init _ (Self)
Self. rowlabels = ["uno", "dos", "tres", "Quatro", "cinco"]
Self. collabels = ["Homer", "Marge", "Bart", "Lisa", "Maggie"]
Def getnumberrows (Self ):
Return 5
Def getnumbercols (Self ):
Return 5
Def isemptycell (self, row, col ):
Return false
Def getvalue (self, row, col ):
Return "(% s, % s)" % (self. rowlabels [row], self. collabels [col])
Def setvalue (self, row, Col, value ):
Pass
Def getcollabelvalue (self, col): # column label
Return self. collabels [col]
Def getrowlabelvalue (self, row): # Row label
Return self. rowlabels [row]
Class testframe (wx. Frame ):
Def _ init _ (Self ):
Wx. Frame. _ init _ (self, none, Title = "Grid table ",
Size = (500,200 ))
Grid = wx. Grid. Grid (Self)
Table = testtable ()
Grid. settable (table, true)
APP = wx. pysimpleapp ()
Frame = testframe ()
Frame. Show ()
App. mainloop ()
By default, labels are displayed in the center. However, you can also use setcolumnlabelalignment (horiz, vert) and setrowlabelalignment (horiz, vert) to change this behavior. The horiz parameter is used to control the horizontal alignment. The values include wx. align_left, wx. align_centre, and wx. align_right. The vert parameter is used to control the vertical aligntop, wx. align_centre, or WX. align_bottom.
the label areas of rows and columns share a set of color and font attributes. You can use the setlabelbackgroundcolour (color), setlabelfont (font), and setlabeltextcolour (color) methods to process these attributes. The color parameter is an example of Wx. colour or wxpython will convert it into a color, such as a color string name. The font parameter is an instance of Wx. font. The get * methods corresponding to set * include getlabelbackgoundcolour (), getlabelfont (), and getlabeltextfont ().