打印经过时间并通过单击按钮重置的计时器

Timer that prints elapsed time and resets with a button click

我有一个 GUI,用户可以在其中单击一个名为“下一组”的按钮,允许他们继续执行下一个任务。我想添加一个计时器,该计时器在他们启动应用程序时立即启动,并且 运行 计时器直到他们按下“下一个设置”按钮。单击时,我希望打印经过的时间并重新启动计时器,直到他们再次按下“下一个设置”按钮。我希望计时器在代码 运行s 时自动启动。目前,“下一组”按钮有两个动作,一个是检索下一组图像,另一个我想包括的动作是重置计时器和打印时间。我也只包含了部分感觉相关的代码,因为它很长。

import time
import tkinter as tk
import csv
from pathlib import Path
import PIL.Image
import PIL.ImageDraw
import PIL.ImageTk
MAX_HEIGHT = 500

IMAGES_PATH = Path("Images")

CSV_LABELS_KEY = "New Labels"
CSV_FILE_NAME_KEY = "FolderNum_SeriesNum"
CSV_BOUNDING_BOX_KEY = "correct_flip_bbox"
counter = 0
timer_id = None
class App(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)  # python3 style
        self.config_paths = ["config 1.yaml", "config 2.yaml", "config 3.yaml"]
        self.config_index = 0
        self.clickStatus = tk.StringVar()
        self.loadedImages = dict()
        self.loadedBoxes = dict()  # this dictionary will keep track of all the boxes drawn on the images
        self.master.title('Slideshow')
        frame = tk.Frame(self)
        tk.Button(frame, text="  Next set ", command=lambda:[self.get_next_image_set(), self.reset()]).pack(side=tk.RIGHT)
        tk.Button(frame, text="  Exit  ", command=self.destroy).pack(side=tk.RIGHT)
        frame.pack(side=tk.TOP, fill=tk.BOTH)
        self.canvas = tk.Canvas(self)
        self.canvas.pack()
        self._load_dataset()

        self.reset()
        self.start_timer = None
        t = time()
        t.start()

    def start_timer(self, evt=None):
        if self._start_timer is not None:
        self._start_timer = time.perf_counter()

    # global counter
    # counter += 1
    # label.config(text=str(counter))
    # label.after(1000, count)

    def reset(self):
        if self._start_timer is None:

        elapsed_time = time.perf_counter() - self._start_timer
        self._start_timer = None
        print('Time elapsed (hh:mm:ss.ms) {}'.format(elapsed_time))


    def _load_dataset(self):
        try:
            config_path = self.config_paths[self.config_index]
            self.config_index += 1
        except IndexError:
            return
        image_data = loadData(config_path)
        # drawing the image on the label
        self.image_data = image_data
        self.currentIndex = 0
        # start from 0th image
        self._load_image()

    def _load_image(self):
        imgName = self.image_data[self.currentIndex]['image_file']
        if imgName not in self.loadedImages:

            self.im = PIL.Image.open(self.image_data[self.currentIndex]['image_file'])
            ratio = MAX_HEIGHT / self.im.height
            # ratio divided by existing height -> to get constant amount
            height, width = int(self.im.height * ratio), int(self.im.width * ratio)
            # calculate the new h and w and then resize next
            self.canvas.config(width=width, height=height)
            self.im = self.im.resize((width, height))
            if self.im.mode == "1":
                self.img = PIL.ImageTk.BitmapImage(self.im, foreground="white")
            else:
                self.img = PIL.ImageTk.PhotoImage(self.im)
            imgData = self.loadedImages.setdefault(self.image_data[self.currentIndex]['image_file'], dict())
            imgData['image'] = self.img
            imgData['shapes'] = self.image_data[self.currentIndex]['shapes']
        # for next and previous so it loads the same image adn don't do calculations again
        self.img = self.loadedImages[self.image_data[self.currentIndex]['image_file']]['image']
        self.canvas.create_image(0, 0, anchor=tk.NW, image=self.img)
        self.show_drag_box()

def loadData(fname):
    with open(fname, mode='r') as f:
        return yaml.load(f.read(), Loader=yaml.SafeLoader)


if __name__ == "__main__":
    data = loadData('config 1.yaml')
    app = App(data)
    app.pack()  # goes here
    app.mainloop()

我使用了日期时间而不是时间,因为减去两个日期时间对象会得到包含小时和分钟的输出,而减去两个时间对象只会得到秒。但是,两者都可以,您可能只需要花时间重新格式化。

读取应用程序启动时的当前时间并存储。每次按下按钮时,从当前时间中减去存储的时间,即可得出已用时间。然后只需存储您的新当前时间,直到按下下一个按钮。下面的代码演示了这一点。

import tkinter as tk
import datetime as dt

class TimeButton(tk.Frame):
  def __init__(self, parent):
    super().__init__(parent)

    # Start timer
    self.current_time = dt.datetime.today()

    # Button
    self.next_set = tk.Button(self, text='Next Set', command = self.clicked)
    self.next_set.pack()

  def clicked(self):
    now = dt.datetime.today()
    time_elapsed = now - self.current_time
    
    print(time_elapsed)
    self.current_time = now


if __name__ == "__main__":
  window = tk.Tk()
  button = TimeButton(window)
  button.pack()
  window.mainloop()