Python中的反行为

Python中的反行为,python,tkinter,Python,Tkinter,我是编程新手,几乎没有几个月的学习时间。我正在尝试构建一个简单的应用程序来计算我投入学习的时间 我正在使用我在这里找到的Tkinter秒表应用程序:“ 为了一些实验和生活质量的改变,我稍微修改了一下 我的想法是添加一个“议程”按钮,在这里我可以看到我投入的总时间(不同的会话)。 “重置”计算为不同的学习课程 议题/问题:“议程”返回奇怪的数字,与两个会话中注册的总秒数无关 import tkinter as Tkinter counter = 0 running = False total =

我是编程新手,几乎没有几个月的学习时间。我正在尝试构建一个简单的应用程序来计算我投入学习的时间

我正在使用我在这里找到的Tkinter秒表应用程序:“ 为了一些实验和生活质量的改变,我稍微修改了一下

我的想法是添加一个“议程”按钮,在这里我可以看到我投入的总时间(不同的会话)。 “重置”计算为不同的学习课程

议题/问题:“议程”返回奇怪的数字,与两个会话中注册的总秒数无关

import tkinter as Tkinter

counter = 0
running = False
total = 0



def counter_label(label):
    def count():
        if running:
            global counter
            global total

            # To manage the intial delay.
            if counter == 0:
                display = "Starting..."
            else:
                display = str(counter)

            label['text'] = display  # Or label.config(text=display)

            # label.after(arg1, arg2) delays by
            # first argument given in milliseconds
            # and then calls the function given as second argument.
            # Generally like here we need to call the
            # function in which it is present repeatedly.
            # Delays by 1000ms=1 seconds and call count again.
            label.after(1000, count)
            counter += 1
            session = counter
            total = session + session

    # Triggering the start of the counter.
    count()


def total_count():
    global total
    display = str(total)
    label['text'] = display


# start function of the stopwatch
def Start(label):
    global running
    running = True
    counter_label(label)
    start['state'] = 'disabled'
    stop['state'] = 'normal'
    reset['state'] = 'normal'


# Stop function of the stopwatch
def Stop():
    global running
    start['state'] = 'normal'
    stop['state'] = 'disabled'
    reset['state'] = 'normal'
    running = False

    if running == False:
        start['text'] = 'Resume'


# Reset function of the stopwatch
def Reset(label):
    global counter
    counter = 0

    # If rest is pressed after pressing stop.
    if running == False:
        reset['state'] = 'disabled'
        label['text'] = 'Welcome!'
        start['text'] = 'Start'

    # If reset is pressed while the stopwatch is running.
    else:
        label['text'] = 'Starting...'


def Agenda():
    global total

    start['state'] = 'normal'
    stop['state'] = 'normal'
    reset['state'] = 'normal'

    global total
    display = str(total)
    label['text'] = display


root = Tkinter.Tk()
root.title("Stopwatch")

# Fixing the window size.
root.minsize(width=250, height=70)
label = Tkinter.Label(root, text="Welcome!", fg="black", font="Verdana 30 bold")
label.pack()
start = Tkinter.Button(root, text='Start',
                       width=15, command=lambda: Start(label))
stop = Tkinter.Button(root, text='Stop',
                      width=15, state='disabled', command=Stop)
reset = Tkinter.Button(root, text='Reset',
                       width=15, state='disabled', command=lambda: Reset(label))
agenda = Tkinter.Button(root, text='Agenda', width=15, command=Agenda)

start.pack()
stop.pack()
reset.pack()
agenda.pack()
root.mainloop()

实际上,您的议程按钮显示如下内容:
agenda=(上次会话持续时间+1)*2
。问题就在这段代码中:

counter += 1
session = counter
total = session + session
在这里,您可以在将值保存为“总计”之前增加计数器,出于某种原因,您还可以使用上一次会话持续时间两次

如果将此部分更改为此部分,您将获得上次会话的持续时间:

session = counter
total = session
counter += 1

无论如何,您可能希望多玩一些,以获得显示总时间的总时间

这个
total=session+session
很合理,为什么要将
计数器的值加倍?阅读最初的“议程”返回的是非常奇怪的数字,在计数器的5秒钟内,我得到的是26或33号。然后我想只记录2次,看看它是否工作正常。在5秒钟内,它返回“6”,在2次每次5秒钟的会话中,它返回“12”。所以两个都要多花一秒钟。编辑:谢谢你的链接,我会用
查看一下。在(1000,
之后,
不能保证每
1000毫秒执行一次。它会在鼠标、键盘等其他事件上延迟。我会研究一下,谢谢。基本代码不是我的,因为我说过它是从网站上删除的,所有的功劳都归他们。