Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的秒表?数到按空格_Python_Stopwatch - Fatal编程技术网

python中的秒表?数到按空格

python中的秒表?数到按空格,python,stopwatch,Python,Stopwatch,我想在Python3.3中制作一个秒表,它会一直计数,直到你按下按钮或其他什么东西,它就会停止计数。这是我的代码: seconds = 0 minutes = 0 continued = 0 while continued != 1: print(minutes, ":", seconds) time.sleep(1) if seconds == 59: seconds = 0 minutes = minutes + 1 else:

我想在Python3.3中制作一个秒表,它会一直计数,直到你按下按钮或其他什么东西,它就会停止计数。这是我的代码:

seconds = 0
minutes = 0
continued = 0
while continued != 1:
    print(minutes, ":", seconds)
    time.sleep(1)
    if seconds == 59:
        seconds = 0
        minutes = minutes + 1
    else:
        seconds = seconds + 1
不使用CTRL+C


我没有使用GUI或任何东西,只使用IDLE在命令提示符下运行纯python代码。

尝试在循环中等待按键,而没有线程或计时器/信号将阻止循环

让主循环在等待按键时继续处理(秒表)的一种方法是通过线程。虽然我从中找到了解决方案,但初步调查还是让我找到了一个答案


如果您想要秒表的GUI,您可能会对以下用于Python3.x的代码感兴趣

#! /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)

if __name__ == '__main__':
    StopWatch.main()
使用诅咒:

import curses
from datetime import datetime

SPACE_KEY = ord(' ')

def run(win):
    curses.echo()
    win.timeout(1000) # Wait at most one second for a key.

    start = datetime.now()
    while True:
        now = datetime.now()
        minutes, seconds = divmod((now - start).total_seconds(), 60)
        win.addstr(0, 0, "%02d:%02d" % (minutes, round(seconds)))

        c = win.getch()
        if c == SPACE_KEY:
            break

curses.wrapper(run)

在Windows下,这不是问题,因为在
msvcrt
模块中有
kbhit
getch

那么问题出在哪里?它将一直运行,直到你用+杀死它。你反对使用
gui
Tkinter
这样的
gui吗?不使用gui或任何东西,纯python代码使用空闲的命令prompt运行看到这个问题什么操作系统?最好使用操作系统计时器,或者简单地使用按键前后的时间差。
import curses
from datetime import datetime

SPACE_KEY = ord(' ')

def run(win):
    curses.echo()
    win.timeout(1000) # Wait at most one second for a key.

    start = datetime.now()
    while True:
        now = datetime.now()
        minutes, seconds = divmod((now - start).total_seconds(), 60)
        win.addstr(0, 0, "%02d:%02d" % (minutes, round(seconds)))

        c = win.getch()
        if c == SPACE_KEY:
            break

curses.wrapper(run)