Python 如何按顺序排列时间?
我是python新手。 基本上我做这个GUI是为了记录一辆车的圈速。一旦赛车完成第一圈,我就要记录第一圈的计时。随后的圈数也需要记录。记录的时间将显示在列表框中。但是,我不知道如何将时间从最快的实验室时间排序到最慢的实验室时间。有人能帮我吗Python 如何按顺序排列时间?,python,sorting,timer,tkinter,Python,Sorting,Timer,Tkinter,我是python新手。 基本上我做这个GUI是为了记录一辆车的圈速。一旦赛车完成第一圈,我就要记录第一圈的计时。随后的圈数也需要记录。记录的时间将显示在列表框中。但是,我不知道如何将时间从最快的实验室时间排序到最慢的实验室时间。有人能帮我吗 from tkinter import * import time class StopWatch(Frame): """ Implements a stop watch frame widget. """
from tkinter import *
import time
class StopWatch(Frame):
""" Implements a stop watch frame widget. """
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
self._start = 0.0
self._elapsedtime = 0.0
self._running = 0
self.timestr = StringVar()
self.e = 0
self.m = 0
self.makeWidgets()
self.laps = []
self.lapmod2 = 0
self.today = time.strftime("%d %b %Y %H-%M-%S", time.localtime())
def makeWidgets(self):
""" Make the time label. """
l1 = Label(self, text='----File Name----')
l1.pack(fill=X, expand=NO, pady=1, padx=2)
self.e = Entry(self)
self.e.pack(pady=2, padx=2)
l = Label(self, textvariable=self.timestr)
self._setTime(self._elapsedtime)
l.pack(fill=X, expand=NO, pady=3, padx=2)
l2 = Label(self, text='----Laps----')
l2.pack(fill=X, expand=NO, pady=4, padx=2)
scrollbar = Scrollbar(self, orient=VERTICAL)
self.m = Listbox(self,selectmode=EXTENDED, height = 10,font=("Helvetica", 12),yscrollcommand=scrollbar.set)
self.m.pack(side=LEFT, fill=BOTH, expand=1, pady=5, padx=2)
scrollbar.config(command=self.m.yview)
scrollbar.pack(side=RIGHT, fill=Y)
def _update(self):
""" Update the label with elapsed time. """
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._timer = self.after(50, self._update)
def _setTime(self, elap):
""" Set the time string to Minutes:Seconds:Hundreths """
minutes = int(elap/60)
seconds = int(elap - minutes*60.0)
hseconds = int((elap - minutes*60.0 - seconds)*100)
self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))
def _setLapTime(self, elap):
""" Set the time string to Minutes:Seconds:Hundreths """
minutes = int(elap/60)
seconds = int(elap - minutes*60.0)
hseconds = int((elap - minutes*60.0 - seconds)*100)
return '%02d:%02d:%02d' % (minutes, seconds, hseconds)
def Start(self):
""" Start the stopwatch, ignore if running. """
if not self._running:
self._start = time.time() - self._elapsedtime
self._update()
self._running = 1
def Stop(self):
""" Stop the stopwatch, ignore if stopped. """
if self._running:
self.after_cancel(self._timer)
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._running = 0
def Reset(self):
""" Reset the stopwatch. """
self._start = time.time()
self._elapsedtime = 0.0
self.laps = []
self._setTime(self._elapsedtime)
def Lap(self):
'''Makes a lap, only if started'''
tempo = self._elapsedtime - self.lapmod2
if self._running:
self.laps.append(self._setLapTime(tempo))
self.m.insert(END, self.laps[-1])
self.m.yview_moveto(1)
self.lapmod2 = self._elapsedtime
def GravaCSV(self):
arquivo = str(self.e.get()) + ' - '
with open(arquivo + self.today + '.txt', 'wb') as lapfile:
for Lap in self.laps:
lapfile.write((bytes(str(Lap) + '\n', 'utf-8')))
def main():
root = Tk()
root.wm_attributes("-topmost", 1)
sw = StopWatch(root)
sw.pack(side=TOP)
Button(root, text='Lap', command=sw.Lap).pack(side=LEFT)
Button(root, text='Start', command=sw.Start).pack(side=LEFT)
Button(root, text='Stop', command=sw.Stop).pack(side=LEFT)
Button(root, text='Reset', command=sw.Reset).pack(side=LEFT)
Button(root, text='Save', command=sw.GravaCSV).pack(side=LEFT)
Button(root, text='Quit', command=root.quit).pack(side=LEFT)
root.mainloop()
if __name__ == '__main__':
main()
通常,经过的时间将存储在一个列表中,然后列表将按照您想要的顺序进行排序。我不知道你是否已经这样做了,但是作为一个现有列表上的简单示例,尝试一下这个,并在文件中看到结果
问题出在代码的哪一部分?不要发布一堆与你要问的问题无关的代码。@Erik Godard代码没有问题。基本上我只知道如何分类。我是python新手,我对中止排序没有任何了解。这就是我需要你们帮助我的原因。你们做了什么来解决这个问题?例如,您在python文档中查找过单词“sort”吗?@BryanOakley我能知道对我上面的代码有帮助的链接是什么吗?仅此网站就有12000多个关于python排序的问题:
def GravaCSV(self):
arquivo = str(self.e.get()) + ' - '
self.laps.sort()
""" next sort as numbers instead of strings (not tested)
and then sort and write laps_as_num to file
laps_as_num=[]
for this_lap in laps:
lap=this_lap.split(":")
laps_as_num.append([int(lap[0]), int(lap[1]), int(lap[2])])
"""
with open(arquivo + self.today + '.txt', 'wb') as lapfile:
for Lap in self.laps:
lapfile.write((bytes(str(Lap) + '\n', 'utf-8')))