一眨眼就初三了,学校目前还没给我们倒计时,于是想自己做一个中考倒计时,顺带激励一下自己。
然后上网搜了一下广州2023中考的时间,也没有准确说,大概在 6.20~6.22左右
但是没关系,不妨碍我完成呢。

说到倒计时最先想到的是python的time库,time库是很好用。后来发现了一个库叫datetime,可以直接用中考的时间减去当前的时间,超级方便。
于是中考倒计时的简陋版就出来了
很简单,也就几行代码
#中考倒计时
import datetime
print('中考倒计时')
now = datetime.datetime.now()
print(' 今天是:',now.strftime('%Y-%m-%d %A'))
time = datetime.datetime(2023,6,20)
print('距离中考开始还有:'+str((time-now).days)+'天')
运行结果如下:
中考倒计时
今天是: 2022-10-30 Sunday
距离中考开始还有:232天
现在我已经知道了还有多少天中考,但我想要的中考倒计时是那种可以在桌面上运行,打开很好看的那种。
于是我想到了python的GUI方向,tkinter就是python自带的模块,很好学
但是tkinter库有一个缺点,就是界面太难看了!!!不少小伙伴都为此困扰过呐
然后我就在C站搜,发现了ttkbootstrap库这个宝藏!

官方网站:ttkbootstrap – ttkbootstrap ttkbootstrap一个实例主题
简约又美观,一下就心动了,毅然决然下了这个库
import time
import datetime
from tkinter import messagebox
import ttkbootstrap as ttk
#定义中考时间,现在时间
exam_time = datetime.datetime(2023,6,20)
now = datetime.datetime.now()
lctime = time.localtime()
lctimee = time.strftime("%Y-%m-%d",lctime)
#建立窗口
root = ttk.Window(title="中考倒计时",themename="litera",size=(500,400),position=(700,100),resizable=None)
#打印今天的时间
tt = ttk.Label(root,text= "今天是"+str(lctimee),font=("仿宋",24),bootstyle="dark")
tt.pack(padx=5, pady=10)
#打印中考倒计时
t = ttk.Label(root,text="距离中考还有"+str((exam_time-now).days)+"天!",font=("华文琥珀",30),bootstyle="warning")
t.pack(padx=5, pady=10)
root.mainloop()
效果如下:

有内味了
再搞个按钮退出吧
#定义退出功能
def quitty():
h=ttk.Label(root,text="中考必胜",font=("楷体",30),bootstyle="info")
h.pack()
messagebox.showinfo("中考倒计时","看你骨骼精奇,定能去个好高中")
time.sleep(1)
root.quit()
#创建退出按钮
btn2 = ttk.Button(root,text='知道了',bootstyle="primary",command=quitty)
btn2.pack()


不错不错(人还是要有点迷信的哈哈哈
后来想了想还是不够,于是在退出那里又加了个烟花,烟花的实现是用pygame库搞的,代码有点长就不放了
最后打包!
打包要用到pyinstaller库
:\>pyinstaller -F -w 中考倒计时.py
-F是打包,-w是去掉cmd黑框
打包完成后放到桌面就能运行了

大概就这样吧
最后祝也要中考的小伙伴和自己 中考顺利,成功上岸!
Original: https://blog.csdn.net/m0_73384722/article/details/127596702
Author: 0717号的专属程序员
Title: python实现中考倒计时
相关阅读
Title: python 画动态图效率_matplotlib 画动态图以及plt.ion()和plt.ioff()的使用详解
学习python的道路是漫长的,今天又遇到一个问题,所以想写下来自己的理解方便以后查看。
在使用matplotlib的过程中,常常会需要画很多图,但是好像并不能同时展示许多图。这是因为python可视化库matplotlib的显示模式默认为阻塞(block)模式。什么是阻塞模式那?我的理解就是在plt.show()之后,程序会暂停到那儿,并不会继续执行下去。如果需要继续执行程序,就要关闭图片。那如何展示动态图或多个窗口呢?这就要使用plt.ion()这个函数,使matplotlib的显示模式转换为交互(interactive)模式。即使在脚本中遇到plt.show(),代码还是会继续执行。下面这段代码是展示两个不同的窗口:
import matplotlib.pyplot as plt
plt.ion() # 打开交互模式
同时打开两个窗口显示图片
plt.figure() #图片一
plt.imshow(i1)
plt.figure() #图片二
plt.imshow(i2)
显示前关掉交互模式
plt.ioff()
plt.show()
在plt.show()之前一定不要忘了加plt.ioff(),如果不加,界面会一闪而过,并不会停留。那么动态图像是如何画出来的,请看下面这段代码,具体的解释就不在这里阐述了,以后有时间再更新:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
def add_layer(inputs,in_size,out_size,activation_funiction=None):
Weights = tf.Variable(tf.random_normal([in_size,out_size]))
biases = tf.Variable(tf.zeros([1,out_size]) +0.1)
Wx_plus_b = tf.matmul(inputs,Weights)+biases
if activation_funiction is None:
outputs = Wx_plus_b
else:
outputs = activation_funiction(Wx_plus_b)
return outputs
x_data = np.linspace(-1,1,300)[:,np.newaxis]
noise = np.random.normal(0,0.05,x_data.shape)
y_data = np.square(x_data)-0.5 +noise
xs = tf.placeholder(tf.float32,[None,1])
ys = tf.placeholder(tf.float32,[None,1])
l1 = add_layer(xs,1,10,activation_funiction=tf.nn.relu)
add output layer
prediction = add_layer(l1,10,1,activation_funiction=None)
the error between prediction and real data
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys – prediction),reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
init =tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x_data,y_data)
plt.ion() #将画图模式改为交互模式
for i in range(1000):
sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
if i%50 ==0:
plt.pause(0.1)
try:
ax.lines.remove(lines[0])
except Exception:
pass
prediction_value = sess.run(prediction,feed_dict={xs:x_data})
lines = ax.plot(x_data,prediction_value,’r-‘,lw=5)
print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
plt.ioff()
plt.show()
在执行上述代码之后,您将看到一条动态拟合数据的曲线,直到训练结束。
[En]
After the execution of the above code, you will see a curve fitting the data dynamically until the end of the training.
下面就来讲讲matplotlib这两种模式具体的区别
在交互模式下:
1、plt.plot(x)或plt.imshow(x)是直接出图像,不需要plt.show()
2、如果在脚本中使用ion()命令开启了交互模式,没有使用ioff()关闭的话,则图像会一闪而过,并不会常留。要想防止这种情况,需要在plt.show()之前加上ioff()命令。
在阻塞模式下:
1、打开一个窗口以后必须关掉才能打开下一个新的窗口。这种情况下,默认是不能像Matlab一样同时开很多窗口进行对比的。
2、plt.plot(x)或plt.imshow(x)是直接出图像,需要plt.show()后才能显示图像
到此这篇关于matplotlib 画动态图以及plt.ion()和plt.ioff()的使用详解的文章就介绍到这了,更多相关matplotlib plt.ion() plt.ioff()内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
Original: https://blog.csdn.net/weixin_42339991/article/details/113496682
Author: 酱油厂的二掌柜
Title: python 画动态图效率_matplotlib 画动态图以及plt.ion()和plt.ioff()的使用详解
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/342167/
转载文章受原作者版权保护。转载请注明原作者出处!