Python秒表示例同时启动所有类实例?

2024-04-27 04:43:16 发布

您现在位置:Python中文网/ 问答频道 /正文

在这个例子中,如何同时启动所有4个秒表?在

这个示例代码已经超过12年了,但它是我通过Google找到的最好的秒表示例。您可以看到我有4个类的实例在使用中。我需要能够在同一时间启动所有实例。修补程序按钮不允许调用多个函数。即使它做到了,它也会是一个函数在下一个函数之前,所以从技术上讲,它们不会同时启动。在

我需要在不同的时间停止每个秒表,但这很容易,只要调用类中的每个stop函数。但我不知道如何同时启动它们。在

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.makeWidgets()

    def makeWidgets(self):
        """ Make the time labels. """
        l = Label(self, textvariable=self.timestr)

        l.pack(fill=X, expand=NO, pady=2, padx=2)


        self._setTime(self._elapsedtime)

    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 Start(self):
        global sw2
        """ 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._setTime(self._elapsedtime)


def main():


    root = Tk()
    sw1 = StopWatch(root)
    sw1.pack(side=TOP)

    sw2 = StopWatch(root)
    sw2.pack(side=TOP)

    sw3 = StopWatch(root)
    sw3.pack(side=TOP)

    sw4 = StopWatch(root)
    sw4.pack(side=TOP)

    Button(root, text='Start', command=sw1.Start).pack(side=LEFT)
    Button(root, text='Stop', command=sw1.Stop).pack(side=LEFT)
    Button(root, text='Reset', command=sw1.Reset).pack(side=LEFT)
    Button(root, text='Quit', command=root.quit).pack(side=LEFT)

    root.mainloop()

if __name__ == '__main__':
    main()

Tags: the函数selfiftimedefrootstart
3条回答

以下程序可能接近您想要的。请注意,由于秒表的启动和停止需要时间,您可能会发现秒表显示的时间之间存在微小差异。在

#! /usr/bin/env python3
import tkinter
import time

class StopWatch(tkinter.Frame):

    @classmethod
    def main(cls):
        tkinter.NoDefaultRoot()
        root = tkinter.Tk()
        root.title('Stop Watch')
        root.resizable(True, False)
        root.grid_columnconfigure(0, weight=1)
        padding = dict(padx=5, pady=5)
        widget = StopWatch(root, **padding)
        widget.grid(sticky=tkinter.NSEW, **padding)
        root.mainloop()

    def __init__(self, master=None, cnf={}, **kw):
        padding = dict(padx=kw.pop('padx', 5), pady=kw.pop('pady', 5))
        super().__init__(master, cnf, **kw)
        self.grid_columnconfigure(1, weight=1)
        self.grid_rowconfigure(1, weight=1)
        self.__total = 0
        self.__label = tkinter.Label(self, text='Total Time:')
        self.__time = tkinter.StringVar(self, '0.000000')
        self.__display = tkinter.Label(self, textvariable=self.__time)
        self.__button = tkinter.Button(self, text='Start', command=self.click)
        self.__label.grid(row=0, column=0, sticky=tkinter.E, **padding)
        self.__display.grid(row=0, column=1, sticky=tkinter.EW, **padding)
        self.__button.grid(row=1, column=0, columnspan=2,
                           sticky=tkinter.NSEW, **padding)

    def click(self):
        if self.__button['text'] == 'Start':
            self.__button['text'] = 'Stop'
            self.__start = time.clock()
            self.__counter = self.after_idle(self.__update)
        else:
            self.__button['text'] = 'Start'
            self.after_cancel(self.__counter)

    def __update(self):
        now = time.clock()
        diff = now - self.__start
        self.__start = now
        self.__total += diff
        self.__time.set('{:.6f}'.format(self.__total))
        self.__counter = self.after_idle(self.__update)

class ManyStopWatch(tkinter.Tk):

    def __init__(self, count):
        super().__init__()
        self.title('Stopwatches')
        padding = dict(padx=5, pady=5)
        tkinter.Button(self, text='Toggle All', command=self.click).grid(
            sticky=tkinter.NSEW, **padding)
        for _ in range(count):
            StopWatch(self, **padding).grid(sticky=tkinter.NSEW, **padding)

    def click(self):
        for child in self.children.values():
            if isinstance(child, StopWatch):
                child.click()

if __name__ == '__main__':
    ManyStopWatch(4).mainloop()

这是我根据布赖恩的想法做的,这个想法是有一个计数器的实例,然后分秒必争。这是可行的,但我还没有想出一种方法,只使用on Getspit函数来抓取每一次。也许通过a,b,c,d然后再问一个if来获得时间?现在我正在用按钮做这个,但是一旦实现,它将通过发生在我正在编写的主程序中的事件抓取它们。如果有人对此有任何改进,请告诉我。谢谢大家的帮助。在

 from Tkinter import *
 import time
 import tkMessageBox

 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.lapastr = StringVar()
    self.lapbstr = StringVar()
    self.lapcstr = StringVar()
    self.lapdstr = StringVar()
    self.makeWidgets()

def makeWidgets(self):
    """ Make the time labels. """
    la = Label(self, textvariable=self.timestr)
    la.pack(fill=X, expand=NO, pady=2, padx=2)
    #self._setTime(self._elapsedtime)

    lb = Label(self, textvariable=self.timestr)
    lb.pack(fill=X, expand=NO, pady=2, padx=2)
    #self._setTime(self._elapsedtime)

    lc = Label(self, textvariable=self.timestr)
    lc.pack(fill=X, expand=NO, pady=2, padx=2)
    #self._setTime(self._elapsedtime)

    ld = Label(self, textvariable=self.timestr)
    ld.pack(fill=X, expand=NO, pady=2, padx=2)

    lsplita = Label(self, textvariable=self.lapastr)
    lsplita.pack(fill=X, expand=NO, pady=2, padx=2)

    lsplitb =Label(self, textvariable=self.lapbstr)
    lsplitb.pack(fill=X, expand=NO, pady=2, padx=2)

    lsplitc = Label(self, textvariable=self.lapcstr)
    lsplitc.pack(fill=X, expand=NO, pady=2, padx=2)

    lsplitd = Label(self, textvariable=self.lapdstr)
    lsplitd.pack(fill=X, expand=NO, pady=2, padx=2)

    self._setTime(self._elapsedtime)

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 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 Getsplita(self):
    """ Stop the stopwatch, ignore if stopped. """
    if self._running:
        self._lapstr = time.time() - self._start
        self._setTime(self._elapsedtime)
        self.lapastr.set(self._lapstr)

def Getsplitb(self):
    """ Stop the stopwatch, ignore if stopped. """
    if self._running:
        self._lapstr = time.time() - self._start
        self._setTime(self._elapsedtime)
        self.lapbstr.set(self._lapstr)

def Getsplitc(self):
    """ Stop the stopwatch, ignore if stopped. """
    if self._running:
        self._lapstr = time.time() - self._start
        self._setTime(self._elapsedtime)
        self.lapcstr.set(self._lapstr)

def Getsplitd(self):
    """ Stop the stopwatch, ignore if stopped. """
    if self._running:
        self._lapstr = time.time() - self._start
        self._setTime(self._elapsedtime)
        self.lapdstr.set(self._lapstr)

def Reset(self):
    """ Reset the stopwatch. """
    self._start = time.time()
    self._elapsedtime = 0.0
    self._setTime(self._elapsedtime)

def main():

     root = Tk()
     sw1 = StopWatch(root)
     sw1.pack(side=TOP)

     Button(root, text='Start', command=sw1.Start).pack(side=LEFT)
     Button(root, text='Stop', command=sw1.Stop).pack(side=LEFT)
     Button(root, text='Reset', command=sw1.Reset).pack(side=LEFT)
     Button(root, text='Quit', command=root.quit).pack(side=LEFT)
     Button(root, text='Get Split A', command=sw1.Getsplita).pack(side=LEFT)
     Button(root, text='Get Split B', command=sw1.Getsplitb).pack(side=LEFT)
     Button(root, text='Get Split C', command=sw1.Getsplitc).pack(side=LEFT)
     Button(root, text='Get Split D', command=sw1.Getsplitd).pack(side=LEFT)
root.mainloop()

if __name__ == '__main__':
     main()

"Tinker [sic] button doesn't allow for calling multiple functions."

不,但它可以调用一个可以调用多个函数的单个函数。在

def start():
    for sw in (sw1, sw2, sw3, sw4):
        sw.Start()

...
Button(root, text='Start', command=start).pack(side=LEFT)

你是对的,不可能同时启动它们。不过,它们之间的距离都在一两毫秒之内,所以在大多数情况下都已经足够了。由于您只显示一整秒的时间粒度,所以计时器应该始终显示相同的时间。在

如果你真的需要它们同步,你需要它们共享相同的开始时间。你可以给秒表一个开始时间。然后,可以计算一次开始时间,并将相同的值传递给所有四个手表:

^{pr2}$

你也可以有一个定时器对象,所有的秒表共享。它们不是秒表,而是“分离计时器”,它们不能控制手表的开始,它们只能记录停止的时间间隔。在

相关问题 更多 >