在python中如何使用按钮作为输入

2024-09-30 03:24:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要创建一个程序来计算汽车在已知距离(100米)内的速度。我需要程序开始计时时,用户按下回车(这应该是模拟当汽车进入监控路段时),停止计时当用户再次按下回车时,然后计算汽车的平均速度。在


Tags: 用户程序距离汽车速度计时平均速度路段
1条回答
网友
1楼 · 发布于 2024-09-30 03:24:12

begin timing when the user presses Enter (this is supposed to simulate when the car enters the monitored road section), stop timing when the user presses Enter again, then calculate the car's average speed.

CLI版本:

#!/usr/bin/env python3
from time import monotonic as timer

input("Press Enter to start timer") # wait for the first Enter from the user
start = timer()
input("Press Enter to stop timer")
elapsed = timer() - start
print("Speed {speed:f} m/s".format(speed=100 / elapsed))

要创建GUI秒表,可以使用tkinter

^{pr2}$

您可以按Enter并单击按钮调用.toggle_button()方法。在

要使主窗口变大,请在root = Tk()行后添加:

root.title('Stopwatch') # set window title
root.geometry('400x300') # set window size
root.columnconfigure(0, weight=1) # children widgets may fill the whole space
root.rowconfigure(0, weight=1)
font.nametofont('TkDefaultFont').configure(size=25) # change default font size

相关问题 更多 >

    热门问题