<blockquote>
<p>At first I thought clicking quickly between start and pause buttons speeds the seconds so I added time.sleep(1) to delay click between the buttons which didn't worked.</p>
</blockquote>
<p>问题是,因为在<code>.aftert()</code>方法调用自身之前有一个延迟,所以多次单击start按钮并创建<code>.after(1000, counter)</code>的多个循环。这意味着有许多<code>.after()</code>循环同时发生。要解决这个问题,您只需要使其在<code>.after()</code>循环进行时不能按开始按钮。你知道吗</p>
<p>要做到这一点,你必须改变你的三个职能</p>
<blockquote>
<p>The <code>counter()</code> function</p>
</blockquote>
<p>我已经更改了代码,因此如果计数器正在运行,<code>start_button</code>将被禁用,当计数器停止运行时,其状态将恢复正常</p>
<pre class="lang-py prettyprint-override"><code>def Counter():
'''Updating hour, minute and seconds'''
global HOUR, MINUTE, SECOND
if PAUSE is False:
start_button['state'] = 'disabled' # setting it to disabled if the counter is running
if SECOND == 59:
if MINUTE == SECOND == 59:
HOUR += 1
if MINUTE == 59:
MINUTE = 0
else:
MINUTE += 1
SECOND = -1
SECOND += 1
Time.config(text='{}:{}:{}'.format(str(HOUR).zfill(2), str(MINUTE).zfill(2), str(SECOND).zfill(2)))
root.after(1000, Counter)
else:
start_button['state'] = 'normal' # setting it to normal if the counter stops
</code></pre>
<blockquote>
<p>The <code>pause()</code> and <code>reset()</code> functions</p>
</blockquote>
<p>必须取消将<code>start_button</code>的状态设置为normal的功能,因为这将允许在最后一个计数器循环完成之前按下<code>start_button</code></p>
<pre class="lang-py prettyprint-override"><code>def pause():
'''Command for PAUSE button'''
global PAUSE
PAUSE = True
pause_button['state'] = 'disabled'
def reset():
'''Command for RESET button'''
global HOUR, MINUTE, SECOND, PAUSE
PAUSE = True
pause_button['state'] = 'disabled'
reset_button['state'] = 'disabled'
Time['text'] = '00:00:00'
HOUR, MINUTE, SECOND = 0, 0, 0
</code></pre>
<p>更新这些函数将删除此错误。
希望这就是你想要的答案:)</p>