单击按钮时关闭wxpython窗口
我有一个wxpythongui,它可以正常工作,当按下“组件缺陷”按钮(函数compMonth)时,它会打开两个窗口,一个gridForm和一个pyplot窗口。但是如果我再次运行相同的按钮而不关闭gridFrom和pyplot窗口,程序就会崩溃。尝试向按钮添加一些内容,以检查其中一个窗口是否打开,如果打开则关闭。不确定从哪里开始单击按钮时关闭wxpython窗口,python,matplotlib,wxpython,Python,Matplotlib,Wxpython,我有一个wxpythongui,它可以正常工作,当按下“组件缺陷”按钮(函数compMonth)时,它会打开两个窗口,一个gridForm和一个pyplot窗口。但是如果我再次运行相同的按钮而不关闭gridFrom和pyplot窗口,程序就会崩溃。尝试向按钮添加一些内容,以检查其中一个窗口是否打开,如果打开则关闭。不确定从哪里开始 import wx import wx.grid as gridlib import csv import datetime import re import num
import wx
import wx.grid as gridlib
import csv
import datetime
import re
import numpy as np
import matplotlib.pyplot as plt; plt.rcdefaults()
from collections import Counter
from operator import itemgetter
import pysftp
import warnings
import os
import openpyxl
from pip._vendor.distlib.util import CSVWriter
import matplotlib
warnings.filterwarnings("ignore")
filepath = os.path.join(os.path.expandvars("%userprofile%"),"Desktop")
powerfile = filepath+"\\Qual History.csv"
#print powerfile
today = datetime.date.today()
month = (today.month - 1)
year = today.year
month2 = month - 2
month3 = month - 3
if month == 1:
month2 = 12
month3 = 11
year2 = year -1
year3 = year2
elif month == 2:
month2 = 1
month3 = 12
year2 = year
year3 = year - 1
month = str(month)
year = str(year)
month2 = str(month2)
year2 = str(year2)
month3 = str(month3)
year3 = str(year3)
#print month, year, month3, year3
regex = re.compile(month +'/../'+year)
regex2 = re.compile(month +'/./'+year)
regex3 = re.compile(month2 +'/../'+year2)
regex4 = re.compile(month2 +'/./'+year2)
regex5 = re.compile(month3 +'/../'+year3)
regex6 = re.compile(month3 +'/./'+year3)
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 = (600,600), pos = wx.Point(1000,150))
panel = wx.Panel(self)
self.myGrid = gridlib.Grid(panel)
self.myGrid.CreateGrid(50, 4)
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.sizer.Add(self.myGrid, 1, wx.CENTER)
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")
class MyForm(wx.Frame):
def __init__(self):
self.datarows = []
wx.Frame.__init__(self, None, wx.ID_ANY, "627 Data", size = (700,400), pos=wx.Point(600,0))
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.month3Button = wx.Button(panel, pos = (350,5), id=wx.ID_ANY, label="Three Month")
self.month3Button.Bind(wx.EVT_BUTTON, self.threeMonth)
self.serialButton = wx.Button(panel, pos = (450,5), id=wx.ID_ANY, label="Get Serial Numbers")
self.serialButton.Bind(wx.EVT_BUTTON, self.serialNumbers)
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.compMonth)
self.metalButton = wx.Button(panel, pos = (250,100), id=wx.ID_ANY, label="METAL DEFECT")
self.metalButton.Bind(wx.EVT_BUTTON, self.compMonth)
self.nffButton = wx.Button(panel, pos = (250,125), id=wx.ID_ANY, label="NFF")
self.nffButton.Bind(wx.EVT_BUTTON, self.compMonth)
self.otherButton = wx.Button(panel, pos = (250,150), id=wx.ID_ANY, label="OTHER")
self.otherButton.Bind(wx.EVT_BUTTON, self.compMonth)
self.vendorButton = wx.Button(panel, pos = (250,175), id=wx.ID_ANY, label="VENDOR PROCESS DEFECT")
self.vendorButton.Bind(wx.EVT_BUTTON, self.compMonth)
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)
# self.myGrid = gridlib.Grid(panel)
# self.myGrid.CreateGrid(20, 4)
#
# self.sizer = wx.BoxSizer(wx.HORIZONTAL)
# self.sizer.Add(self.myGrid, 1, wx.CENTER)
# panel.SetSizer(self.sizer)
#----------------------------------------------------------------------
def lastMonth(self, event):
frame = gridForm()
frame.Destroy()
partNumber = self.partText.GetValue()
if partNumber != "":
csvfile = open(powerfile)
datareader = csv.reader(csvfile, delimiter = ',')
compCount = 0
emcCount = 0
metalCount = 0
nffCount = 0
otherCount = 0
vendorCount = 0
self.datarows=[]
for row in datareader:
if re.match(regex,row[3]) or re.match(regex2,row[3]):
self.datarows.append(row)
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))
def threeMonth(self, event):
partNumber = self.partText.GetValue()
if partNumber != "":
csvfile = open(powerfile)
datareader = csv.reader(csvfile, delimiter = ',')
compCount = 0
emcCount = 0
metalCount = 0
nffCount = 0
otherCount = 0
vendorCount = 0
self.datarows=[]
for row in datareader:
if re.match(regex,row[3]) or re.match(regex2,row[3]) or re.match(regex3,row[3]) or re.match(regex4,row[3]) or re.match(regex5,row[3]) or re.match(regex6,row[3]):
self.datarows.append(row)
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))
def compMonth(self, event):
####this is where it should check for the windows and close them
partNumber = self.partText.GetValue()
csvfile = open(powerfile)
datareader = csv.reader(csvfile, delimiter = ',')
compCount = 0
compFails = []
compFinal = {}
performance = []
serials =[]
#
for row in self.datarows:
if (row[1] == partNumber and row[9] == 'COMPONENT DEFECT' ):
compCount += 1
serials.append(row[0])
if row[7] == '':
compFails.append(row[13])
else:
compFails.append(row[7])
#print compFails
c = Counter(compFails).items()
c.sort(key=itemgetter(1))
compFails.sort(key=Counter(compFails).get, reverse=True)
#print compFails
#print compFails
for item in compFails:
#print item
compFinal[item] = compFails.count(item)
#print compFinal
count = len(compFinal)
#print count
frame = gridForm()
frame.myGrid.SetCellValue(0,0, "Failure")
frame.myGrid.SetCellValue(0,1, "Count")
frame.myGrid.SetCellValue(0,2, "Percent Total")
y=1
x=0
totalCount = sum(compFinal.values())
for key in compFinal:
percentage = "%.0f%%" % (100 * compFinal[key]/totalCount)
frame.myGrid.SetCellValue(y,x,key)
frame.myGrid.SetCellValue(y,(x + 1),str(compFinal[key]))
frame.myGrid.SetCellValue(y,(x + 2), str(percentage))
y += 1
performance.append(compFinal[key])
labels, values = zip(*c)
indexes = np.arange(len(labels))
plt.bar(indexes, values, align='center',)
plt.xticks(indexes, labels, rotation = 'vertical')
plt.ylabel('Number of Failures')
plt.title('627 Pareto for ' + partNumber)
frame.Show()
plt.show()
def serialNumbers(self, event):
#plt.close(1)
partNumber = self.partText.GetValue()
csvfile = open(powerfile)
datareader = csv.reader(csvfile, delimiter = ',')
compCount = 0
serials =[]
#
for row in self.datarows:
if (row[1] == partNumber and row[9] == 'COMPONENT DEFECT' ):
compCount += 1
serials.append(row[0])
print serials
writer = csv.writer(open(filepath+'\serials.csv', 'wb'))
for word in serials:
writer.writerow([word])
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
是否尝试捕获错误?否,不确定如何执行…在name==“main”之后添加一个try/except子句并将其打印到stdout或写入文件或使用日志记录模块从命令行(终端)运行程序将提供错误消息。我从命令行运行该程序,但未提供任何错误,GUI只是冻结,然后它将继续(“无响应”),我将不得不强制关闭。