Search code examples
pythonuser-interfacewxpython

Writing to a a second frame from first Frame using wxPython


First attempt at a GUI using wxpython. I am trying to write from the main frame "MyForm" to a second Frame which is a gridlayout form. I keep running into: AttributeError: 'MyForm' object has no attribute 'myGrid'

Below is my code, error happens at line 120. I relize that it is stuck in MyForm, I am just not sure how to get out of it.

import wx
import wx.grid as gridlib
import csv
import datetime
import re
today = datetime.date.today()
month = str(today.month -1)
year = str(today.year)
regex = re.compile(month +'/../'+year)
regex2 = re.compile(month +'/./'+year)
rootCause = ['COMPONENT DEFECT','EMC PROCESS DEFECT','METAL DEFECT','NFF','OTHER','VENDOR PROCESS DEFECT']
class gridForm(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Drill Down Data", size = (1000,1000))

        panel = wx.Panel(self)

        self.myGrid = gridlib.Grid(panel)
        self.myGrid.CreateGrid(12, 8)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.myGrid, 1, wx.EXPAND)
        panel.SetSizer(self.sizer)


class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "627 Data", size = (1000,1000))
        panel = wx.Panel(self, wx.ID_ANY)

        self.monthButton = wx.Button(panel, pos = (250,5), id=wx.ID_ANY, label="Last Month")
        self.monthButton.Bind(wx.EVT_BUTTON, self.lastMonth)

        self.compButton = wx.Button(panel, pos = (250,50), id=wx.ID_ANY, label="COMPONENT DEFECT")
        self.compButton.Bind(wx.EVT_BUTTON, self.compMonth)
        self.emcButton = wx.Button(panel, pos = (250,75), id=wx.ID_ANY, label="EMC PROCESS DEFECT")
        self.emcButton.Bind(wx.EVT_BUTTON, self.lastMonth)
        self.metalButton = wx.Button(panel, pos = (250,100), id=wx.ID_ANY, label="METAL DEFECT")
        self.metalButton.Bind(wx.EVT_BUTTON, self.lastMonth)
        self.nffButton = wx.Button(panel, pos = (250,125), id=wx.ID_ANY, label="NFF")
        self.nffButton.Bind(wx.EVT_BUTTON, self.lastMonth)
        self.otherButton = wx.Button(panel, pos = (250,150), id=wx.ID_ANY, label="OTHER")
        self.otherButton.Bind(wx.EVT_BUTTON, self.lastMonth)
        self.vendorButton = wx.Button(panel, pos = (250,175), id=wx.ID_ANY, label="VENDOR PROCESS DEFECT")
        self.vendorButton.Bind(wx.EVT_BUTTON, self.lastMonth)

        self.partLabel = wx.StaticText(panel,100, 'Part number', pos = (15,10))
        self.partText = wx.TextCtrl(panel, pos = (100,5), size = (100,-1))
        self.partText.SetInsertionPoint(0)
        self.compText = wx.TextCtrl(panel, pos = (100,50), size = (100,-1))
        self.compText.SetInsertionPoint(0)
        self.emcText = wx.TextCtrl(panel, pos = (100,75), size = (100,-1))
        self.emcText.SetInsertionPoint(0)
        self.metalText = wx.TextCtrl(panel, pos = (100,100), size = (100,-1))
        self.metalText.SetInsertionPoint(0)
        self.nffText = wx.TextCtrl(panel, pos = (100,125), size = (100,-1))
        self.nffText.SetInsertionPoint(0)
        self.otherText = wx.TextCtrl(panel, pos = (100,150), size = (100,-1))
        self.otherText.SetInsertionPoint(0)
        self.vendorText = wx.TextCtrl(panel, pos = (100,175), size = (100,-1))
        self.vendorText.SetInsertionPoint(0)
        # self.Bind(wx.EVT_BUTTON, self.onButton, button)



    #----------------------------------------------------------------------
    def lastMonth(self, event):
        partNumber = self.partText.GetValue()
        if partNumber != "":
            csvfile = open('C:\\627\Qual History.csv')
            datareader = csv.reader(csvfile, delimiter = ',')
            compCount = 0
            emcCount = 0
            metalCount = 0
            nffCount = 0
            otherCount = 0
            vendorCount = 0
            for row in datareader:
                if re.match(regex,row[3]) or re.match(regex2,row[3]):
                    if (row[1] == partNumber and  row[9] == 'COMPONENT DEFECT' ):
                        compCount += 1

                    elif (row[1] == partNumber and  row[9] == 'EMC PROCESS DEFECT' ):
                        emcCount += 1
                    elif (row[1] == partNumber and  row[9] == 'METAL DEFECT' ):
                        metalCount += 1
                    elif (row[1] == partNumber and  row[9] == 'NFF' ):
                        nffCount += 1
                    elif (row[1] == partNumber and  row[9] == 'OTHER' ):
                        otherCount += 1
                    elif (row[1] == partNumber and  row[9] == 'VENDOR PROCESS DEFECT' ):
                        vendorCount += 1
            self.compText.SetValue(str(compCount))
            self.emcText.SetValue(str(emcCount))
            self.metalText.SetValue(str(metalCount))
            self.nffText.SetValue(str(nffCount))
            self.otherText.SetValue(str(otherCount))
            self.vendorText.SetValue(str(vendorCount))

            print compCount, emcCount, metalCount, nffCount, otherCount, vendorCount
    def compMonth(self, event):
        partNumber = self.partText.GetValue()
        csvfile = open('C:\\627\Qual History.csv')
        datareader = csv.reader(csvfile, delimiter = ',')
        compCount = 0
        compFails = []
        compFinal = {}
        for row in datareader:
            if re.match(regex,row[3]) or re.match(regex2,row[3]):
                if (row[1] == partNumber and  row[9] == 'COMPONENT DEFECT' ):
                    compCount += 1
                    compFails.append(row[7])
        print compFails
        for item in compFails:
            compFinal[item] = compFails.count(item)
        print compFinal
        count = len(compFinal)
        print count
        self.myGrid.gridForm.SetCellValue(0,0, "Failure")  #here is where ir starts
        self.myGrid.SetCellValue(0,1, "Count")
        self.myGrid.SetCellValue(0,3, "Percent Total")
        frame = gridForm()
        frame.Show()


# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

Solution

  • myGrid is an attribute of gridForm class, that is why self.myGrid won't work from within MyForm, as in MyForm self is an instance of the class MyForm and not gridForm. There are many alternatives to what you want to accomplish. Two of these are:

    • you can wrap the calls to SetCellValue into a method of gridForm and call the new method as frame.SetCellValueWrapper()

    as follows:

    class gridForm(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY, "Drill Down Data", size = (1000,1000))
    
            panel = wx.Panel(self)
    
            self.myGrid = gridlib.Grid(panel)
            self.myGrid.CreateGrid(12, 8)
    
            self.sizer = wx.BoxSizer(wx.VERTICAL)
            self.sizer.Add(self.myGrid, 1, wx.EXPAND)
            panel.SetSizer(self.sizer)
    
        def SetCellValueWrapper(self):
            self.myGrid.SetCellValue(0,0, "Failure")  #here is where ir starts
            self.myGrid.SetCellValue(0,1, "Count")
            self.myGrid.SetCellValue(0,3, "Percent Total")
    

    def compMonth(self, event):
        frame = gridForm()
        frame.SetCellValueWrapper()
        frame.Show()
    
    • or you can access myGrid directly and call the method.

    like this

    def compMonth(self, event):
        frame = gridForm()
        frame.myGrid.SetCellValue(0,0, "Failure")  #here is where ir starts
        frame.myGrid.SetCellValue(0,1, "Count")
        frame.myGrid.SetCellValue(0,3, "Percent Total")
        frame.Show()