首页 > 解决方案 > Tkinter 时间按钮不输出

问题描述

我试图记录按钮按下之间的时间,然后将该时间附加到列表中,但是当我按下stop_time_forward按钮时,我没有得到它打印任何东西。对此的任何帮助都将是惊人的。我的代码:

import time
from tkinter import *

initial = 0
top = Tk()
command_time_list = []

#start forward
def start_time_forward():
   global initial
   print("Timer Start")
   initial = time.time()
   return initial

def stop_time_forward():
   final = time.time()
   time_elapsed = final - initial
   command_time_list.append('a')
   command_time_list.append(time_elapsed)
   return command_time_list

forward_output = stop_time_forward()
print(forward_output)

forward_end = Button(top, text ="Forward Time Stop", command = stop_time_forward)
forward_start = Button(top, text ="Forward Time Start", command = start_time_forward)

forward_end.pack()
forward_start.pack()
top.mainloop()

标签: pythonpython-3.xlisttkintertime

解决方案


您需要添加的只是在调用按钮 forward_end 时调用的 stop_time_forward 中的打印函数

import time
from tkinter import *

initial = 0
top = Tk()
command_time_list = []

#start forward
def start_time_forward():
   global initial
   print("Timer Start")
   initial = time.time()
   return initial

def stop_time_forward():
   final = time.time()
   time_elapsed = final - initial
   command_time_list.append('a')
   command_time_list.append(time_elapsed)
   print(command_time_list) # here is the fix
   return command_time_list

forward_output = stop_time_forward()
print(forward_output)

forward_end = Button(top, text ="Forward Time Stop", command = stop_time_forward)
forward_start = Button(top, text ="Forward Time Start", command = start_time_forward)

forward_end.pack()
forward_start.pack()
top.mainloop()

推荐阅读