我有一个软件的python接口,它存储excel中的相机数据。软件工作是当我点击准备相机开始记录和状态显示(运行),然后当我点击相同的选项卡它停止。现在我想在excel中写入当前数据。当前它正在保存所有以前的值
这将是非常有帮助的,如果我能得到帮助,因为我是一个新的Python。
import wx
import wx.lib.activex
import csv
import comtypes.client
class EventSink(object):
def __init__(self, frame):
self.counter = 0
self.frame = frame
def DataReady(self):
self.counter +=1
self.frame.Title= "DataReady fired {0} times".format(self.counter)
class MyApp( wx.App ):
def OnClick(self,e):
rb_selection = self.rb.GetStringSelection()
if rb_selection == "WinCam":
data = self.gd.ctrl.GetWinCamDataAsVariant()
data = [[x] for x in data]
else:
p_selection = self.cb.GetStringSelection()
if p_selection == "Profile_X":
data = self.px.ctrl.GetProfileDataAsVariant()
data = [[x] for x in data]#csv.writerows accepts a list of rows where each row is a list, a list of lists
elif p_selection == "Profile_Y":
data = self.py.ctrl.GetProfileDataAsVariant()
data = [[x] for x in data]
**elif p_selection =="data_info":
while self.data18 ==1:
data= [self.data1,self.data2,self.data3,self.data4,self.data5,self.data6,self.data7,self.data8,self.data9,self.data10,self.data11,self.data12,self.data13,self.data14,self.data15,self.data16,self.data17,self.data18]
b=data
file = open('C:\Users\Namrata\data.csv', 'w')
with file:
a = ['Clip Level a', 'Clip level b', 'Major','Minor','Mean','Eff_2W','Ellip','Orient','Crosshair','Xc','Yc','Centroid','Rc','ADC Peak','Exposure','image zoom','plateau uniformity']
zip(a,b)
out = csv.writer(file)
out.writerows(zip(a,b))
file.close()**
else:
datax = self.px.ctrl.GetProfileDataAsVariant()
datay = self.py.ctrl.GetProfileDataAsVariant()
data = [list(row) for row in zip(datax,datay)]#Makes a list of lists; X1 with Y1 in a list, X2 with Y2 in a list etc...
filename = self.ti.Value
with open(filename, 'w') as fp:
w = csv.writer(fp, delimiter=',')
w.writerows(data)
def __init__( self, redirect=False, filename=None ):
wx.App.__init__( self, redirect, filename )
self.frame = wx.Frame( parent=None, id=wx.ID_ANY,size=(1000,760), title='Python Interface to DataRay')
#Panel
p = wx.Panel(self.frame, wx.ID_ANY)
#Get Data
self.gd = wx.lib.activex.ActiveXCtrl(p, 'DATARAYOCX.GetDataCtrl.1')
#The methods of the object are available through the ctrl property of the item
self.gd.ctrl.StartDriver()
self.counter = 0
sink = EventSink(self.frame)
self.sink = comtypes.client.GetEvents(self.gd.ctrl, sink)
#Button Panel
bp = wx.Panel(parent=self.frame, id=wx.ID_ANY, size=(300, 400))
b1 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(280,25), pos=(20, 0),axID='DATARAYOCX.ButtonCtrl.1')
b1.ctrl.ButtonID =297
self.data18= b1.ctrl.GetParameter()
b1.ctrl.GetParameter()#Id's for some ActiveX controls must be set
b2 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(140,25), pos=(5, 30),axID='DATARAYOCX.ButtonCtrl.1')
b2.ctrl.ButtonID =294
self.data1= b2.ctrl.GetParameter()
b3 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(140,25), pos=(150,30),axID='DATARAYOCX.ButtonCtrl.1')
b3.ctrl.ButtonID =295
data2= b3.ctrl.GetParameter()
b4 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(140,25), pos=(5,60),axID='DATARAYOCX.ButtonCtrl.1')
b4.ctrl.ButtonID =180
data3= b4.ctrl.GetParameter()
b5 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(140,25), pos=(150, 60),axID='DATARAYOCX.ButtonCtrl.1')
b5.ctrl.ButtonID =181
self.data4= b5.ctrl.GetParameter()
b6 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(140,25), pos=(5, 90),axID='DATARAYOCX.ButtonCtrl.1')
b6.ctrl.ButtonID =182
data5= b6.ctrl.GetParameter()
b7 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(140,25), pos=(150, 90),axID='DATARAYOCX.ButtonCtrl.1')
b7.ctrl.ButtonID =95
data6= b7.ctrl.GetParameter()
b8 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(140,25), pos=(5, 120),axID='DATARAYOCX.ButtonCtrl.1')
b8.ctrl.ButtonID =177
data7= b8.ctrl.GetParameter()
b9 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(140,25), pos=(150, 120),axID='DATARAYOCX.ButtonCtrl.1')
b9.ctrl.ButtonID =179
data8= b9.ctrl.GetParameter()
b10 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(140,25), pos=(5,150),axID='DATARAYOCX.ButtonCtrl.1')
b10.ctrl.ButtonID =302
data9= b10.ctrl.GetParameter()
b11= wx.lib.activex.ActiveXCtrl(parent=bp,size=(140,25), pos=(150, 150),axID='DATARAYOCX.ButtonCtrl.1')
b11.ctrl.ButtonID =171
data10= b11.ctrl.GetParameter()
b12 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(140,25), pos=(5, 180),axID='DATARAYOCX.ButtonCtrl.1')
b12.ctrl.ButtonID =172
data11= b12.ctrl.GetParameter()
b13 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(140,25), pos=(150, 180),axID='DATARAYOCX.ButtonCtrl.1')
b13.ctrl.ButtonID =298
data12= b13.ctrl.GetParameter()
b14 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(140,25), pos=(5, 210),axID='DATARAYOCX.ButtonCtrl.1')
b14.ctrl.ButtonID =425
data13= b14.ctrl.GetParameter()
b15 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(140,25), pos=(150, 210),axID='DATARAYOCX.ButtonCtrl.1')
b15.ctrl.ButtonID =183
data14= b15.ctrl.GetParameter()
b16 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(140,25), pos=(5, 240),axID='DATARAYOCX.ButtonCtrl.1')
b16.ctrl.ButtonID =409
data15= b16.ctrl.GetParameter()
b17 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(140,25), pos=(150, 240),axID='DATARAYOCX.ButtonCtrl.1')
b17.ctrl.ButtonID =301
data16= b17.ctrl.GetParameter()
b18 = wx.lib.activex.ActiveXCtrl(parent=bp,size=(280,25), pos=(5, 270),axID='DATARAYOCX.ButtonCtrl.1')
b18.ctrl.ButtonID =291
data17= b18.ctrl.GetParameter()
#Custom controls
t = wx.StaticText(bp, label="File:", pos=(5, 300))
self.ti = wx.TextCtrl(bp, value="C:\Users\Public\Documents\output.csv", pos=(30, 300), size=(170, -1))
self.rb = wx.RadioBox(bp, label="Data:", pos=(5, 330), choices=["Profile", "WinCam","Data Info"])
self.cb = wx.ComboBox(bp, pos=(5,380), choices=[ "Profile_X", "Profile_Y", "data_info","Both"])
self.cb.SetSelection(0)
myb = wx.Button(bp, label="Write", pos=(150,380))
myb.Bind(wx.EVT_BUTTON, self.OnClick)
myd = wx.Button(bp, label="Dialog", pos=(150,330))
myd.Bind(wx.EVT_BUTTON, self.OnClick)
#Pictures
pic = wx.lib.activex.ActiveXCtrl(parent=self.frame,size=(300,400),axID='DATARAYOCX.CCDimageCtrl.1')
tpic = wx.lib.activex.ActiveXCtrl(parent=self.frame,size=(300,400), axID='DATARAYOCX.ThreeDviewCtrl.1')
palette = wx.lib.activex.ActiveXCtrl(parent=self.frame,size=(10,250), axID='DATARAYOCX.PaletteBarCtrl.1')
#Profiles
self.px = wx.lib.activex.ActiveXCtrl(parent=self.frame,size=(300,300),axID='DATARAYOCX.ProfilesCtrl.1')
self.px.ctrl.ProfileID=22
self.py = wx.lib.activex.ActiveXCtrl(parent=self.frame,size=(300,300),axID='DATARAYOCX.ProfilesCtrl.1')
self.py.ctrl.ProfileID = 23
self.ax = wx.lib.activex.ActiveXCtrl(parent=self.frame,size=(150,360),axID='DATARAYOCX.getdataCtrl.1')
#Formatting
row1 = wx.BoxSizer(wx.HORIZONTAL)
row1.Add(window=bp,flag=wx.RIGHT, border=10)
row1.Add(pic)
row1.Add(window=tpic, flag=wx.LEFT, border=10)
row2 = wx.BoxSizer(wx.HORIZONTAL)
row2.Add(self.px, 0, wx.RIGHT, 100)# Arguments: item, proportion, flags, border
row2.Add(self.py)
col1 = wx.BoxSizer(wx.VERTICAL)
col1.Add(sizer=row1, flag=wx.BOTTOM, border=10)
col1.Add(sizer=row2, flag=wx.ALIGN_CENTER_HORIZONTAL)
self.frame.SetSizer(col1)
self.frame.Show()
if __name__ == "__main__":
app = MyApp()
app.MainLoop()
要实时保存数据,可以这样做:
首先创建一个持续触发的wx.timer
:
self.timer = wx.Timer(self)
self.timer.start(#here you have to specify the amount of milliseconds the timer sleep before fire again)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
然后创建OnTimer()
函数来将数据存储在excel文件中:
from openpyxl import load_workbook
def OnTimer(self, event):
data = #here you get your data
wb = load_workbook('path/to/yout/excel/file.xlsx')
sheet = wb.active()
#here you have to play with cells rows and cols by inserting data and saving,
#for more info go to the below link
openpyxl文档