python - 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()
解决方案
您需要添加的只是在调用按钮 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()
推荐阅读
- python - Pymongo:如何检查字段是否存在
- ruby-on-rails - 为什么安装包后 gem 没有出现在本地 gem 中?
- python - Python - 在函数外部创建的对象在内部完全可访问?
- vuejs2 - Vue路由器问题
- java - 如何使用 Spring Cloud Stream 在 RabbitMQ 的队列中添加功能“x-delayed-type:direct”?
- c# - 使用 .xlsm c#
- java - javafx - 延迟beetwen键释放和listview选择单元格
- android - 不同分辨率下的Android可绘制项目大小
- python - 如何使用 pygame 和 pyttsx 让动画人脸说话
- r - R:循环中的 gsub 替换名称