python倒数计时器问题(不起作用)

时间:2019-08-15 16:18:56

标签: python python-3.x tkinter

我用tkinter做了一个倒数计时器。但是,当插入“ if(state):”时,不显示计时器。我想在第一次不启动时显示计时器。还有一个问题是在操作过程中停止按钮不起作用。

对不起我的英语

我删除了“ if(state):”和“ state = False”。然后显示计时器,但它从头开始。

import tkinter as tk
import time
from tkinter import *


root = tk.Tk()
root.title("Timer")
root.geometry("980x640")

sec = 100


def main():
     if (state):
        for x in range(sec, -1, -1):
             lbl1["text"] = str(formatTime(x))
             root.update()
             time.sleep(1)

def formatTime(x):
    minutes, seconds_rem = divmod(x, 60)
    return "%02d:%02d" % (minutes, seconds_rem)


def start():
    global state
    state = True

def stop():
    global state
    state = False

def reset():
    global sec
    sec = 1800

def add1min():
    global sec
    sec += 60

def rm1min():
    global sec
    if sec > 60:
       sec -= 60

state = False


startButton = tk.Button(root, text='Start', command=start)
startButton.pack(fill = 'x', padx=30, side = 'left')
startButton.pack()

stopButton = tk.Button(root, text='Stop', command=stop)
stopButton.pack(fill = 'x', padx=30, side = 'left')
stopButton.pack()

resetButton = tk.Button(root, text='Reset', command=reset)
resetButton.pack(fill = 'x', padx=30, side = 'left')
resetButton.pack()

add1minButton = tk.Button(root, text='+1 min', command=add1min)
add1minButton.pack(fill = 'x', padx=30, side = 'left')
add1minButton.pack()


rm1minButton = tk.Button(root, text='-1 min', command=rm1min)
rm1minButton.pack(fill = 'x', padx=30, side = 'left')
rm1minButton.pack()


lbl1 = Label()
lbl1.pack(fill=BOTH, expand=1)
main()
root.mainloop()

没有错误代码

1 个答案:

答案 0 :(得分:2)

直到进入Start()之后,您才进入main()函数,因此state仍然为False,而您的main()函数不会显示您的计时器。按下按钮后,尝试再次致电main()