我是编程新手,几乎没有几个月的学习时间。我正在尝试构建一个简单的应用程序来计算我投入学习的时间。
我正在使用我在这里找到的 Tkinter 秒表应用程序:"https://www.geeksforgeeks.org/create-stopwatch-using-python/"我稍微修改了一下,以进行一些实验和 QoL 更改。
我的想法是添加一个"议程"按钮,我可以在其中查看我投入的总时间(不同的会话)。"重置"计为不同的学习会话。
问题/问题:"议程"返回奇怪的数字,与两个会话中注册的总秒数无关。
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 = (last_session_duration +1) * 2
。问题出在这段代码中:
counter += 1
session = counter
total = session + session
在这里,您在将值保存到总计之前递增计数器,并且出于某种原因,您也使用last_session_duration两次。
如果将此部分更改为此部分,您将获得上次会话的持续时间:
session = counter
total = session
counter += 1
无论如何,您可能想玩更多以获得显示总时间的总时间。