0

如何将格式设置为 hh:mm:ss?而不是 mm:ss:centisecond?

import Tkinter as tk

def update_timeText():
    if (state):
        global timer
        timer[2] += 1

        if (timer[2] >= 100):
            timer[2] = 0
            timer[1] += 1
        if (timer[1] >= 60):
            timer[0] += 1
            timer[1] = 0
        timeString = pattern.format(timer[0], timer[1], timer[2])
        timeText.configure(text=timeString)
    root.after(10, update_timeText)

def start():
    global state
    state = True

def pause():
    global state
    state = False

def reset():
    global timer
    timer = [0, 0, 0]
    timeText.configure(text='00:00:00')

def exist():
    root.destroy()

state = False

root = tk.Tk()
root.wm_title('Simple Kitchen Timer Example')

timer = [0, 0, 0]
pattern = '{0:02d}:{1:02d}:{2:02d}'

timeText = tk.Label(root, text="00:00:00", font=("Helvetica", 150))
timeText.pack()

startButton = tk.Button(root, text='Start', command=start)
startButton.pack()

pauseButton = tk.Button(root, text='Pause', command=pause)
pauseButton.pack()

resetButton = tk.Button(root, text='Reset', command=reset)
resetButton.pack()

quitButton = tk.Button(root, text='Quit', command=exist)
quitButton.pack()

update_timeText()
root.mainloop()
4

3 回答 3

3
# make: 00:00:00 stop watch timer format
def update_timeText():
  if (state):
    global timer
    timer[2] += 1
    if (timer[2] >= 60):
        timer[2] = 0
        timer[1] += 1
    if (timer[1] >= 60):
        timer[0] += 1
        timer[1] = 0
    timeString = pattern.format(timer[0], timer[1], timer[2])
    return timeString

# Step 1        
global state      
global timer    
state = True
timer = [0, 0, 0]
pattern = '{0:02d}:{1:02d}:{2:02d}'

# Step 2
print update_timeText()
print update_timeText()
print update_timeText()
print update_timeText()

输出:

00:00:01
00:00:02
00:00:03
00:00:04
于 2013-11-08T11:08:49.010 回答
0

如果你想要 HR:MIN:SEC:CSEC 使用:

import Tkinter as tk

def update_timeText():
    if (state):
        global timer
        timer[2] += 1

        if (timer[2] >= 100):
            timer[2] = 0
            timer[1] += 1
        if (timer[1] >= 60):
            timer[0] += 1
            timer[1] = 0
        timeString = pattern.format(timer[0], timer[1], timer[2])
        timeText.configure(text=timeString)
    root.after(10, update_timeText)

def start():
    global state
    state = True

def pause():
    global state
    state = False

def reset():
    global timer
    timer = [0, 0, 0]
    timeText.configure(text='00:00:00')

def exist():
    root.destroy()

state = False

root = tk.Tk()
root.wm_title('Simple Kitchen Timer Example')

timer = [0, 0, 0]
pattern = '{0:02d}:{1:02d}:{2:02d}'

timeText = tk.Label(root, text="00:00:00", font=("Helvetica", 150))
timeText.pack()

startButton = tk.Button(root, text='Start', command=start)
startButton.pack()

pauseButton = tk.Button(root, text='Pause', command=pause)
pauseButton.pack()

resetButton = tk.Button(root, text='Reset', command=reset)
resetButton.pack()

quitButton = tk.Button(root, text='Quit', command=exist)
quitButton.pack()

update_timeText()
root.mainloop()
于 2014-12-16T17:27:14.667 回答
0

只需将 更改 root.after(10, update_timeText)root.after(1000, update_timeText)。然后,您的秒表应该以秒为单位计数。

于 2015-06-23T16:01:52.297 回答