November 18th, 2004, 11:56 AM
How do you open a txt file and populate wxGrid?
Looking for help on wxGrid. I need to open a txt file and populate a grid of size say (2,2) with data thats in the txt file.
example txt contents:
the only data that needs to be in the cells are what are in blue. So cell (0,0) would display the value 8A and cell (0,1) would display 8B. Then for the next row (1,0) would get FF and so on.
Any help on setting up the code would be great. I'm not that familiar with Python yet but I'm trying to get a crash course in it right now for work and this is very interesting stuff.
Thanks again for any help.
I have this same setup using individual txt boxes and was having a hard time figureing out how to do this same thing and seperate the data to individual txt boxes and thought wxGrid might be easier to display and save the data.
November 18th, 2004, 06:48 PM
ok, I did alot of digging on the site and through the docs and I was able to get it to create the cells, and populate the cells doing this in the EVT_MENU for the open command from the menu I created.
My file contained the sample data of:
def __init__(self, parent):
self.FileName=None # Default Filename for Save
def OnMenu1Items0Menu(self, event): # Open txt File
dlg = wxFileDialog(self, "Choose a file", ".", "", "*.*", wxOPEN)
if dlg.ShowModal() == wxID_OK:
filename = dlg.GetPath()
f = open(filename, 'r')
data = f.read()
lines = data.split('\n')
str00 = lines[6:8]
str01 = lines[8:10]
str02 = lines[6:8]
str03 = lines[8:10]
self.grid1.SetCellValue(0, 0, str00)
self.grid1.SetCellValue(0, 1, str01)
self.grid1.SetCellValue(1, 0, str02)
self.grid1.SetCellValue(1, 1, str03)
which took the 89 AE and AF 44 and put them into the corresponding cells like I wanted.
Now how would I save it back to the file if I changed one of the variables like change AF to A9?
I tried to use similar code for the save event but im not doing something correctly because it keeps giving me an atribute error. I will keep digging around and maybe I will come across something in my further research.
I would actually like to simplify the code in a loop or something because I need a actual cell grid of (16,16) and I would really rather not have to call each cell when I want to put data into it or write to it.