我是编程新手,几乎没有几个月的学习经验。我正在尝试构建一个简单的应用程序来计算我投入学习的时间。
我正在使用在这里找到的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()
答案 0 :(得分:0)
实际上,您的议程按钮显示如下内容:agenda = (last_session_duration +1) * 2
。问题在这段代码内:
counter += 1
session = counter
total = session + session
在这里,您需要先增加计数器,然后再将值保存为总计,并且由于某种原因,您还会使用last_session_duration两次。
如果将此部分更改为此,您将获得上一会话的持续时间:
session = counter
total = session
counter += 1
无论如何,您可能想要玩得更多,以显示总计时间。