0

我的程序是一个秒表,基本上,当您单击stopImage它时,它应该在它所在的时间停止。但是,我不确定如何在不重置时钟的情况下做到这一点。此外,如果我在startImage按钮外部单击两次,时钟就会启动,如果我然后单击stopImage,它会重置为零。我使用的图形包是Zelle 的 graphics

from graphics import *
import time

win = GraphWin('Stopwatch', 600, 600)
win.yUp()

timer = Text(Point(300, 260), '')
timer.setSize(30)
timer.draw(win)
timer.setText('00:00:00')

stopWatchImage = Image(Point (300, 300), "stopwatch.png")
startImage = Image(Point (210, 170), "startbutton.png")
stopImage = Image(Point (390, 170), "stopbutton.png")
lapImage = Image(Point (300, 110), "lapbutton.png")
stopWatchImage.draw(win)
startImage.draw(win)
stopImage.draw(win)
lapImage.draw(win)

def pressed(pt, image):
    width = image.getWidth()
    height = image.getHeight()
    anchor = image.getAnchor()
    x1 = (anchor.getX()) - (.5 * width)
    x2 = (anchor.getX()) + (.5 * width)
    y1 = (anchor.getY()) - (.5 * height)
    y2 = (anchor.getY()) + (.5 * height)
    return(x1 <= pt.getX() <= x2 and y1 <= pt.getY() <= y2)

def timeFormat():
    sec = 0
    minute = 0
    hour = 0
    timeDisplay = "{0:02d}:{1:02d}:{2:02d}".format(hour, minute, sec)
    timer.setText(timeDisplay)
    check = win.getMouse()
    task = 'start'
    '''
    if pressed(check, startImage):
        task = 'start'
    '''
    while task == 'start':
        task = 'start'
        time.sleep(1)
        sec += 1
        if sec == 60:
            sec = 0
            minute += 1
        if minute == 60:
            sec = 0
            minute = 0
            hour += 1
        timeDisplay = "{0:02d}:{1:02d}:{2:02d}".format(hour, minute, sec)
        timer.setText(timeDisplay)
        check = win.checkMouse()
        if check != None:
            if pressed(check, stopImage):
                task = 'stop'
                print('stoppressed')
                main()
                '''
                if pressed(check, startImage):
                    task = 'start'
                    print('start2pressed')
                    timeFormat()
                '''
            if pressed(check, lapImage):
                sec = 0
                minute = 0
                hour = 0
                task = 'stop'
                timer.setText('00:00:00')
                print('lappressed')
                timeFormat()

def main():
    check = win.checkMouse()
    if check!= None:
        while not pressed(win.checkMouse(), startImage):
            continue

    timeFormat()

main()
4

0 回答 0