As per your directions Paul, I have put up it up on the wiki
Hi Paul! I came across your article on the WIKI. The same article is included in the demo in wxPython installation. I have been tinkering around for quite sometime with wxPython and God, is the learning curve is just vertical?!
The URL to your article for reference:
Here it is: http://wiki.wxpython.org/index.cgi/DrawingOnGridColumnLabel
I wrote to Robin Dunn and asked him if any book was being written on wxPython to which he replied that an associate of his is writing the book and it it 1/3 rd finished. I hope the book comes out by the end of this year.
I had written a small program in foxpro and want to duplicate a certain feature of its GUI using wxPython.
The following single line command does in foxpro which I know is going to take several dozen lines in Python; but thats the challenge.
BROW FIELDS ROLLNUMBER, NAME, MARKS FREEZE MARKS
What this command line does is that, it creates a grid with three columns named ROLLNUMBER, NAME and MARKS and as many rows as there are records in the first field. The command FREEZE MARKS causes only the MARKS column to be editable. When data entry is complete the key combination CTRL+W causes the window to close and the data entered to be written to the database.
Essentially, the following steps needs to be worked out in python.
- Create the table with three columns
ROLL NUMBER,NAMEandMARKS - Populate the rows of the table with the data pulled from a database. This has to be dynamically populated.
- Make the first two columns
read-only. - Set focus on the first cell in the third column.
- Set events so that the
Enterkey causes the cell below to be focussed. If it reaches the last row, it remains there. Theupanddownarrow key causes the focus to shift to the upper and lower cell respectively. As before, the focus does not go beyond the first and the last cell. Theleftand therightarrow keys to be disabled. - When the table is closed either with X on the window title bar or with a key combination, preferably
CTRL + W, the data from the cells is read, validated, formatted and written to the database.So far, I have just reached the first step! The code snippet is given below:
self.colLabels = ['Roll No.', 'Name', 'Marks',]
self.dataTypes = [wxGRID_VALUE_NUMBER,
wxGRID_VALUE_STRING,
wxGRID_VALUE_NUMBER
]
self.data = [
[1, "RUPESH PRADHAN", ""],
[2, "KATE BLANCHET", ""],
[3, "ROBBIE WILLIAMS", ""]
]For the second step, I was using the follwing lines of code to dynamically generate the data to be placed in the table for the purpose of testing. Of course, at a later stage, the data will be pulled from a database. So here is the code:
datastring = ""
for i in range(1,10):
datastring = datastring + "["+str(i)+",'Some Name',''"+"]"
if i<9:
datastring = datastring + ","
print datastring
This produces an output given below:
[1,'Some Name',''],[2,'Some Name',''],[3,'Some Name',''],[4,'Some Name',''],[5,'Some Name',''],[6,'Some Name',''],[7,'Some Name',''],[8,'Some Name',''],[9,'Some Name','']Now, I want to assign it to
self.data as given below:
self.data = [datastring]
But, it does not work.
How do I go on from here?
Any help would be appreciated.
Regards Rupesh Pradhan
Paul's on vacation, but I'll take a stab at it.
You can't use strings to create lists in the manner you outline above. It's best to use the native list functions to accomplish this. Try this:
# Clear the property
self.data = []
for i in range(1, 10):
# Create the row-level list
row = [i]
# Append the name
row.append('Some Name')
#Now add that row to the data
self.data.append( row )
print self.data
Hope that helps you! - ed
Rupesh: This works nicely.
I have added two more lines to put data into the third column
row.append('Some Name')
# add the data row
row.append('')
#Now add that row to the data
I think you should take a look at Dabo, which already draws a grid based on the underlying data from the database. The grid in Dabo doesn't currently allow direct editing, but it would not be hard to modify (and we intend to make columns editable in the future). --pkm
Rupesh: Downloaded.