使用wxPython从第一帧写入第二帧

时间:2017-02-21 15:52:35

标签: python user-interface wxpython

首次尝试使用wxpython进行GUI。我试图从主框架" MyForm"到第二帧,这是一个gridlayout形式。我一直遇到: AttributeError:' MyForm'对象没有属性' myGrid'

下面是我的代码,错误发生在第120行。我重申它被困在MyForm中,我只是不确定如何摆脱它。

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()

1 个答案:

答案 0 :(得分:0)

myGridgridForm类的属性,这就是self.myGrid无法在MyForm内工作的原因,如MyForm self }是类MyForm的实例,而不是gridForm。您想要实现的目标有很多替代方案。其中两个是:

  • 您可以将对SetCellValue的调用打包成gridForm的方法,并将新方法称为frame.SetCellValueWrapper()

如下:

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()
  • 或者您可以直接访问myGrid并调用该方法。
像这样

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()