使用wxPython从第一帧写入第二帧
使用wxpython首次尝试GUI。我试图从主框架“MyForm”写入第二个框架,即gridlayout表单。我经常遇到: AttributeError:“MyForm”对象没有属性“myGrid” 下面是我的代码,错误发生在第120行。我意识到它被困在我的身体里,我只是不知道如何摆脱它使用wxPython从第一帧写入第二帧,python,user-interface,wxpython,Python,User Interface,Wxpython,使用wxpython首次尝试GUI。我试图从主框架“MyForm”写入第二个框架,即gridlayout表单。我经常遇到: AttributeError:“MyForm”对象没有属性“myGrid” 下面是我的代码,错误发生在第120行。我意识到它被困在我的身体里,我只是不知道如何摆脱它 import wx import wx.grid as gridlib import csv import datetime import re today = datetime.date.today() mo
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()
myGrid
是gridForm
类的一个属性,这就是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")
- 或者您可以直接访问
,并调用该方法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()
现在想想,如果我能在“MyForm”中插入网格,我就不会有这个问题了,这比使用第二个框架更容易。这方面的任何帮助都会起作用。关于如何选择框架打开的窗口位置,有什么想法吗?例如,我希望第一个帧在屏幕的左侧打开,第二个帧在右侧打开。您可以向
wx.Frame
的构造函数提供位置参数,例如:wx.Frame.\uuuuuu init\uuuuuuuuuuu(self,None,wx.ID\uany,“深入数据”,size=(10001000),pos=wx.Point(x,y))
再次感谢您,那太完美了。还有一个问题:),函数lastMonth会打开gridform,但是如果在不关闭gridform的情况下再次运行该函数,则程序会崩溃。在lastMonth函数中是否有关闭gridform窗口(如果打开)的方法?
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()