暂停并继续秒表 [英] Pause and continue stopwatch

查看:26
本文介绍了暂停并继续秒表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建秒表.我已经做到了,但我想随时暂停并继续时间.我尝试了一些事情,但我不知道该怎么做.有没有人能解释一下怎么做?

I am trying to create stopwatch. I have done it but I would like to pause and continue the time whenever I want. I have tried some things but I have no idea how to do it. Is there anybody who would explain me how to do it?

import time, tkinter

canvas=tkinter.Canvas(width=1900,height=1000,bg='white')
canvas.pack()
canvas.create_text(950,300,text=':',font='Arial 600')
def write(x_rec,y_rec,x_text,rep):
    canvas.create_rectangle(x_rec,0,y_rec,750,outline='white',fill='white')
    if rep<10:
        canvas.create_text(x_text,400,text='0'+str(rep),font='Arial 600')
    else:
        canvas.create_text(x_text,400,text=str(rep),font='Arial 600')
def write_minutes(rep):
    write(0,900,450,rep)
def write_seconds(rep):
    write(1000,1900,1450,rep)
def time(num,remember):
    while remember[0]<num:
        remember[1]+=1
        write_seconds(remember[1])
        if remember[1]==60:
            remember[0]+=1
            remember[1]=0
            write_seconds(remember[1])
            write_minutes(remember[0])
        canvas.update()
        canvas.after(1000)



remember=[0,0]
num=1
write_seconds(remember[1])
write_minutes(remember[0])

time(5,remember)

推荐答案

我想不出一个干净的方法来修改你的代码来做你想做的事,所以决定将秒表作为一个类来实现程序更面向对象,避免使用一堆全局变量.

I couldn't figure-out a clean way to modify your code to do what you want, so decided to implement the stop watch as a class to make the program more object-oriented and avoid the use of a bunch of global variables.

我还没有对此进行彻底的测试,但是已经有足够的方法可以让您了解这个想法.另请注意,我将 Resume 按钮更改为可在该按钮和 Pause 按钮之间切换的按钮.这种方法不需要添加第三个.

I haven't tested this thoroughly, but there's enough of it working to give you the idea. Note also that I changed a Resume button into one that toggles itself between that and being Pause button. This approach made adding a third one unnecessary.

我注意到可能存在潜在问题,因为随着显示更新,越来越多的对象不断添加到 Canvas.对于短期运行的 StopWatch 实例来说,这应该不是问题,但可能会导致长时间运行的问题.

I noticed what could be potential problem because more and more objects keep getting added to the Canvas as the display is updated. This shouldn't be a problem for a short-running StopWatch instance, but might cause issues with a long-running one.

为了避免这种情况,我修改了代码以更新现有的相应 Canvas 文本对象(如果有).我还将 Button 移到顶部,StopWatch 上方.

To avoid this, I modified the code to update the existing corresponding Canvas text object if there is one. I also moved the Buttons to the top, above the StopWatch.

from functools import partial
import time
import tkinter as tk

PAUSE, RESUME = 0, 1  # Button states.


# Button callback functions.
def _toggle(callback):
    toggle_btn.state = 1 - toggle_btn.state  # Toggle button state value.
    toggle_btn.config(**toggle_btn_states[toggle_btn.state])
    callback()

def _stop():
    stopwatch.cancel_updates()
    toggle_btn.config(state=tk.DISABLED)
    stop_btn.config(state=tk.DISABLED)


class StopWatch:
    def __init__(self, parent, run_time, width, height):
        self.run_time = run_time
        self.width, self.height = width, height
        self.font = 'Arial 600'

        self.canvas = tk.Canvas(parent, width=width, height=height, bg='white')
        self.canvas.pack()
        self.canvas.create_text(950, 300, text=':', font=self.font)
        self.running, self.paused = False, False
        self.after_id = None

    def start(self):
        self.elapsed_time = 0  # In seconds.
        self._display_time()
        self.after_id = self.canvas.after(1000, self._update)
        self.running, self.paused = True, False

    def _update(self):
        if self.running and not self.paused:
            if self.elapsed_time == self.run_time:
                _stop()  # Sets self.running to False.
                self.canvas.bell()  # Beep.
            else:
                self.elapsed_time += 1
                self._display_time()

        if self.running:  # Keep update process going.
            self.after_id = self.canvas.after(1000, self._update)

    def _display_time(self):
        mins, secs = divmod(self.elapsed_time, 60)
        self._write_seconds(secs)
        self._write_minutes(mins)

    def _write_minutes(self, mins):
        self._write(0, 900, 450, 'mins', mins)

    def _write_seconds(self, secs):
        self._write(1000, 1900, 1450, 'secs', secs)

    def _write(self, x_rec, y_rec, x_text, tag, value):
        text = '%02d' % value
        # Update canvas text widget if it has non-empty text.
        if self.canvas.itemcget(tag, 'text'):
            self.canvas.itemconfigure(tag, text=text)
        else:   # Otherwise create it.
            self.canvas.create_text(x_text, 400, text=text, tag=tag, font=self.font)

    def pause_updates(self):
        if self.running:
            self.paused = True

    def resume_updates(self):
        if self.paused:
            self.paused = False

    def cancel_updates(self):
        self.running, self.paused = False, False
        if self.after_id:
            self.canvas.after_cancel(self.after_id)
            self.after_id = None

# main
root = tk.Tk()

# Create a Frame for Buttons (allows row of them to be centered).
button_frame = tk.Frame(root)
button_frame.pack(side=tk.TOP)

# Create StopWatch and configure buttons to use it.
stopwatch = StopWatch(root, 5, 1900, 1000)
toggle_btn = tk.Button(button_frame)
toggle_btn_states = {}

# Dictionary mapping state to button configuration.
toggle_btn_states.update({
    PAUSE: dict(
        text='Pause', bg='red', fg='white',
        command=partial(_toggle, stopwatch.pause_updates)),
    RESUME: dict(
        text='Resume', bg='green', fg='white',
        command=partial(_toggle, stopwatch.resume_updates))
})

toggle_btn.state = PAUSE
toggle_btn.config(**toggle_btn_states[toggle_btn.state])
toggle_btn.pack(side=tk.LEFT, padx=2)

stop_btn = tk.Button(button_frame, text='Stop', bg='blue', fg='white', command=_stop)
stop_btn.pack(side=tk.LEFT, padx=2)

stopwatch.start()
root.mainloop()

这是显示秒表运行的屏幕截图:

Here's a screenshot showing the stopwatch running:

这篇关于暂停并继续秒表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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