python - 停止时钟,从绿色开始,在 5 分钟变为黄色,然后在 10 分钟后再次变为红色
问题描述
所以我需要一个停止时钟,用户界面的背景从绿色开始,然后在 5 分钟后变为黄色,然后在 10 分钟后变为红色。我正在使用 Tkinter GUI 和源代码:使用 python 创建秒表。
这是我对代码的修改
#importing the required libraries
import tkinter as Tkinter
from datetime import datetime
counter = 9000000
running = False
def counter_label(label):
def count():
if running:
global counter
# To manage the intial delay.
if counter==9000000:
display="Starting..."
else:
tt = datetime.fromtimestamp(counter)
string = tt.strftime("%H:%M:%S")
display=string
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
# Triggering the start of the counter.
count()
# 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
# Reset function of the stopwatch
def Reset(label):
global counter
counter=9000000
# If rest is pressed after pressing stop.
if running==False:
reset['state']='disabled'
# If reset is pressed while the stopwatch is running.
else:
label['text']='Starting...'
root = Tkinter.Tk()
root.title("Stopwatch")
# Fixing the window size.
root.minsize(width=500, height=300)
label = Tkinter.Label(root, text=" ", fg="black", font="Verdana 200 bold")
label.pack()
f = Tkinter.Frame(root)
start = Tkinter.Button(f, text='Start', width=6, command=lambda:Start(label),font="Verdana 50 bold")
stop = Tkinter.Button(f, text='Stop',width=6,state='disabled', command=Stop,font="Verdana 50 bold")
reset = Tkinter.Button(f, text='Reset',width=6, state='disabled', command=lambda:Reset(label),font="Verdana 50 bold")
f.pack(anchor = 'center',pady=5)
start.pack(side="left")
stop.pack(side ="left")
reset.pack(side="left")
root.configure(bg='red')
root.mainloop()
我所做的大部分工作都是修剪一些不必要的部分并更改 1 或 2 个小东西。我不确定如何使背景颜色随时间自动变化。我相信通过添加一个if
语句来说明计时器何时小于 5 分钟会很简单root.configure(bg='green')
。
但是我不确定什么变量本身实际上随着停止时钟而变化。
解决方案
您可以将颜色更改添加到计数器循环并使用counter
更改颜色:
if counter==9000000:
display="Starting..."
else:
tt = datetime.fromtimestamp(counter)
string = tt.strftime("%H:%M:%S")
display=string
if counter > 9000600: # 10 minutes
root.configure(bg='red')
elif counter > 9000300: # 5 minutes
root.configure(bg='yellow')
.......
root.configure(bg='green') # start green
root.mainloop()
推荐阅读
- amazon-web-services - 与不同 AWS 账户的用户共享 AMI
- php - 未定义变量:将 laravel 升级到 5.7 后视图中的 data_test
- python - 散景:在多个人物上使用相同的工具效果
- java - 如何将draw.io集成到java网站?
- python - 在 python os.walk 脚本的命令行中定义参数
- boolean-logic - 具有大量变量的布尔代数表达式简化
- xml - 导入谷歌表格的xpath问题
- xamarin - xamarin 表单中数据网格的无限滚动
- javascript - 以干净的方式包装承诺
- android - 用于安卓登录的 Snapkit