accessing a field from an imported class

20 views
Skip to first unread message

liamd...@gmail.com

unread,
Aug 15, 2016, 2:02:33 AM8/15/16
to wxPython-users

Building a personal software in python/wxPython connected to a MySQL DB

My problem:

trying to write one form that will access classes from an imported file. The form accesses at any one time 4 related tables on the database.


Three(3) of these tables are always the same. The fourth can be one of two tables.


The tables that are always accessed : session, cardio and notes The tables that can vary are: physical or machine


I have placed the data fields in wx.Panel classes for each of the tables mentioned above. 

The mainForm's menu calls the input form with a reference to either the physical or the machine parameter. The appropriate form is displayed with its data fields.


My problem is I can't access these imported fields to load data from the database.


Sections of my code:

Mainform: dailyResultsV2.py

import inputForm as IF

# Menu Event:

def OnShowBikeRecs(self, event):

self.bikeedit = IF.inputForm(None, -1, "Bike Record Management", size=(925,650), name="bike")
    self.bikeedit.Show()
self.bikeedit.Center()

Called Form: inputForm.py

class inputForm(wx.Frame):
def __init__(self, parent, *args, **kwargs):
    wx.Frame.__init__(self, parent, *args, **kwargs)

    self.EXERCISE = kwargs['name']


    # create a panel
    self.panel = wx.Panel(self)
    self.panel.BackgroundColour = (200, 230, 250)
    self.filterSetup()
    self.sessionSection = IFC.tblSession(self.panel,-1)
    self.sessionSection.BackgroundColour = (200, 230, 250)
    if self.EXERCISE == 'walk':
        self.physicalSection = IFC.tblPhysical(self.panel,-1)
    else:
        self.machineSection = IFC.tblMachine(self.panel,-1)

    self.cardioSection = IFC.tblcardio(self.panel,-1)
    self.notesSection = IFC.tblNotes(self.panel,-1)
    self.buttonSetup() 
    self.topsizer()

The preceding class creates an input form which will include all the fields from the physical table or the machine table.

My problem how to I access these fields to add data from the database or update the database. (One side note this is a physical training database).

example of the imported classes: inputFormClasses.py

class tblMachine(wx.Panel):    
def __init__(self, parent, *args, **kwargs):
    wx.Panel.__init__(self, parent, *args, **kwargs)

    self.BackgroundColour = (200, 230, 250)

    # The columns
    # machine_ID - not displayed        machine_session_FK  - not displayed     machine_level  - not displayed
    # machine_Type_FK   machine_Distance    machine_AvgSpeed   machine_MaxSpeed     machine_Duration     machine_ODO     machine_RunFK - ComboBox           

    self.runNames = []      # ComboBox List

    self.machineLabel_st = wx.StaticText(self, -1, "Machine", style=wx.ALIGN_LEFT)
    self.machineType_st = wx.StaticText(self, -1, "Type", style=wx.ALIGN_LEFT)
    self.machineType_tc = wx.TextCtrl(self, -1, value="Biking",  style=wx.TE_CENTRE)
    self.machineDistance_st = wx.StaticText(self, -1, "Distance", style=wx.ALIGN_LEFT)
    self.machineDistance_tc = wx.TextCtrl(self, -1, style=wx.TE_CENTRE)
    self.machineAvgSpeed_st = wx.StaticText(self, -1, "Avg. Speed", style=wx.ALIGN_LEFT)
    self.machineAvgSpeed_tc = wx.TextCtrl(self, -1, style=wx.TE_CENTRE)
    self.machineMaxSpeed_st = wx.StaticText(self, -1, "Max Speed", style=wx.ALIGN_LEFT)
    self.machineMaxSpeed_tc = wx.TextCtrl(self, -1, style=wx.TE_CENTRE)
    self.machineDuration_st = wx.StaticText(self, -1, "Duration", style=wx.ALIGN_LEFT)
    self.machineDuration_mtc = masked.TimeCtrl(self, -1, fmt24hr=True)
    self.machineODO_st = wx.StaticText(self, -1, "Odometer", style=wx.ALIGN_LEFT)
    self.machineODO_tc = wx.TextCtrl(self, -1, style=wx.TE_CENTRE)
    self.machineRun_st = wx.StaticText(self, -1, "Run", style=wx.ALIGN_LEFT)
    self.machineRun_cbo = wx.ComboBox(self, -1, choices = self.getRunNames(self.runNames), style=wx.CB_READONLY)  # get the list of runs for comboBox

    # create machine sizer 
    self.MachineSizer = wx.GridBagSizer(hgap=5, vgap=5)

How to I access for example: self.machineType_tc on my form ?   The code self.machineType_tc.write(str(dataList[4])), does not work.   inputForm.py does not recognise self.machineType_tc.  
 
 

liamd...@gmail.com

unread,
Aug 15, 2016, 9:40:36 AM8/15/16
to wxPython-users
 Solved:  I was too quick to post this question.  I solved my own problem.
The imported class's control can be accessed by using the variable which instantiated (not sure this is the right word) the class.  To use the above example, to add or retrieve data from "self.machineType_tc" use the variable that uses the class tblMachine :  self.machineSection.  So self.machineSection.machineType_tc.write() will add data to the control and .GetValue() to retrieve the control's contents.


Reply all
Reply to author
Forward
0 new messages