Python 如何使用tkinter每隔10秒将数据收集到同一个CSV文件中
我正在使用python作为GUI,希望收集40分钟范围内的温度。我希望程序每10秒将数据保存到同一个csv文件中的csv。出于某种原因,我的代码保存了10个csv文件或10秒,而不是1个包含10个数据点的csv文件Python 如何使用tkinter每隔10秒将数据收集到同一个CSV文件中,python,loops,csv,user-interface,tkinter,Python,Loops,Csv,User Interface,Tkinter,我正在使用python作为GUI,希望收集40分钟范围内的温度。我希望程序每10秒将数据保存到同一个csv文件中的csv。出于某种原因,我的代码保存了10个csv文件或10秒,而不是1个包含10个数据点的csv文件 def start(): # Stores Data Initial Columns with open(txtName + time.strftime('%H:%M:%S', time.localtime()) + '.csv', 'a') as data: o
def start(): # Stores Data Initial Columns
with open(txtName + time.strftime('%H:%M:%S', time.localtime()) + '.csv',
'a') as data:
output = csv.writer(data)
row = ['Time' , 'Left Hand Temp' , 'Right Hand Temp' , 'Fluid
Temp Into Handle' , 'Fluid Temp Out of Handle' ]
output.writerow(row)
def collect(): # Stores Data File Name With User Input
with open(txtName + time.strftime('%H:%M:%S', time.localtime()) + '.csv',
'a') as data:
##### THIS IS MY ATTEMPT AT A 10 second loop #######
for everysecond in range(60):
if everysecond % 10 is 0:
output = csv.writer(data)
rows = [time.strftime('%H:%M:%S', time.localtime()),
sensor1.temperature, sensor2.temperature,
sensor3.read_temp_c(),sensor4.read_temp_c()]
output.writerow(rows)
#### I Think this is where the code for the 10 second loop should go, For
some reason i cant get it to take data with respect to computer time.
#Window
topFrame= Frame(root)
topFrame.pack()
bottomframe = Frame(root)
bottomframe.pack(side=BOTTOM)
#################### Buttons ##########################
button1 = Button(topFrame,text="Start Work-out", bg ='blue', command =
start)
button2 = Button(topFrame,text="Collect", bg ='green', command = collect)
exitbutton = Button(root, bg = 'red', text='Stop Work-out', command =
root.destroy)
button1.pack(side = LEFT)
button2.pack()
#
应每隔10秒将数据保存/更新到同一csv文件,范围为(60)内的每一秒:将在瞬间完成。您可以使用
after
方法每隔10秒调用collect
函数,因为您已将当前时间用作输出文件名的一部分,单击开始
和收集
按钮时,您将创建一个新文件。但是,当您单击“收集”按钮时,输出文件中应写入六条记录。