#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    17
    Rep Power
    0

    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:

    :00108A8B9F
    :0020FF8887

    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.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    17
    Rep Power
    0
    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.

    Code:
        def __init__(self, parent):
            self._init_ctrls(parent)
            self.FileName=None  # Default Filename for Save  
            self.grid1.CreateGrid(2,2)
    
        def OnMenu1Items0Menu(self, event): # Open txt File
            dlg = wxFileDialog(self, "Choose a file", ".", "", "*.*", wxOPEN)
            try:
                if dlg.ShowModal() == wxID_OK:
                    filename = dlg.GetPath()
                    f = open(filename, 'r')
                    data = f.read()
                    f.close
                    self.FileName=filename
                    
                    lines = data.split('\n')
                    str00 = lines[0][6:8]
                    str01 = lines[0][8:10]
                    str02 = lines[1][6:8]
                    str03 = lines[1][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)
            
            finally:
                dlg.Destroy()
    My file contained the sample data of:
    :0010089AE98
    :00200AF44B4

    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.

IMN logo majestic logo threadwatch logo seochat tools logo