Python 秒表示例 - 同时启动所有类实例? [英] Python stopwatch example - starting all class instances at the same time?

查看:34
本文介绍了Python 秒表示例 - 同时启动所有类实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In this example how can I start all 4 stop watches at the same time?

此示例代码已有 12 年的历史,但它是我通过 Google 找到的最好的秒表示例.您可以看到我有 4 个正在使用的类实例.我需要能够同时启动所有实例.Tinker 按钮不允许调用多个函数.即使这样做了,它也将是下一个函数之前的一个函数,因此从技术上讲,它们不会全部同时启动.

This example code is over 12 years old but it is the best stopwatch example I have been able to find via Google. You can see that I have 4 instances of the class in use. I need to be able to start all the instances at the exact same time. Tinker button doesn't allow for calling multiple functions. Even if it did it would be one function before the next so technically they wouldn't all start at the exact same time.

我需要在不同的时间停止每个秒表,但只需调用类中的每个 Stop 函数即可.但我不知道如何同时启动它们.

I will need to stop each stopwatch at different times but that is easy by just calling each Stop function in the class. But I can't figure out how to start them all at the same time.

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

推荐答案

以下是我最终根据 Bryan 的想法所做的事情,即拥有一个计数器实例,然后将时间分开.这有效,但我还没有想出一种方法来每次都使用 Getspit 函数来抓取.也许传入 a,b,c,d 然后一个 if 来获取时间?现在我正在用按钮来做这件事,但一旦实现,它将通过我正在编写的主程序中发生的事件来抓取它们.如果有人对此有任何改进,请告诉我.感谢大家的帮助.

Here is what I ended up doing based on Bryan's idea of having one instance of the counter and then taking splits of the time. This works but I have not figured out a way to just use on Getspit function to grab each time. Maybe passing in the a,b,c,d and then an if to get the time? Right now I am doing this with buttons but once implemented it will be grabbing them via events that happen in the main program I am writing. If anyone has any improvements on this please let me know. Thanks to everyone for the help.

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

这篇关于Python 秒表示例 - 同时启动所有类实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆