python日出日落时间实现和详解
目录
- 前期准备内容
- 如何打包成.exe
- 具体实现代码
前期准备内容
下载python3.11版本的python环境(官网下载地址:https://www.python.org/)
鼠标指向
Downloads
(不用点击)会弹出下拉框,直接点击windows
下的Python 3.11
即可下载。
然后点击开始下载,Python
就下载好了。(也可以点windows
去下载想要的版本)
安装python
勾选
Add Python 3.11 to PATH
,选择Install Now
。等待
Python
安装完。点击下方limit
,就完成了安装。
下载PyCharm工具
官网下载地址:https://www.jetbrains.com/pycharm/
具体安装教程可以百度找破解版,这里就不详细描述。
如何打包成.exe
打包成.exe
命令; dd.py
替换成自己的文件名。
pyinstaller -F -w dd.py
可以在PyCharm
工具里打包,如下图(也可以在cmd
中找到该文件位置,进行该命令进行打包)。
打包成功后会在dist
文件夹下。
具体实现代码
看成品效果。
以下是完整代码。
tkinter
是创建页面使用的包
astral
是计算时间的包
from babel import numbers
这个必须要引入,不然打包成.exe
的时候启动会报没有该包的错误导致无法启动,我们这里直接引入后打包进去
re
是正则的验证
import tkinter
import tkinter.messagebox
from astral import LocationInfo
from astral import sun
import datetime
from tkcalendar import DateEntry
from babel import numbers
import re# 具体实现逻辑
def sunsetQuery():# 城市名name = 'XiChong'# 国家region = 'China'# 时区timezone = 'Asia/Harbin'# 维度# latitude = 22.484786latitudes = entryLatitude.get().strip()# 经度# longitude = 114.549965longitudes = entryLongitude.get().strip()# 时间日期 Y意思是YYYY y意思是YYdate = datetime.datetime.strptime(labelCal.get(), "%m/%d/%y")# 经纬度验证if len(latitudes) == 0 or len(latitudes) == None:return tkinter.messagebox.showinfo('erro', "请输入维度坐标")elif not bool(re.match(r'^(\+|-)?(?:90(?:(?:\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\.[0-9]{1,6})?))$', latitudes)):return tkinter.messagebox.showinfo('erro', "请输入正确维度坐标")if len(longitudes) == 0 or len(longitudes) == None:return tkinter.messagebox.showinfo('erro', "请输入经度坐标")elif not bool(re.match(r'^(\+|-)?(?:180(?:(?:\.0{1,6})?)|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:(?:\.[0-9]{1,6})?))$', longitudes)):return tkinter.messagebox.showinfo('erro', "请输入正确经度坐标")location = LocationInfo(name, region, timezone, latitudes, longitudes)# 获取当前时间日期sunrise = sun.sunrise(location.observer, date, location.timezone)# 计算相应时间的日出sunrise_new = str(datetime.datetime.strftime(sunrise, "%Y-%m-%d %H:%M:%S"))sunset = sun.sunset(location.observer, date, location.timezone)# 计算相应时间的日落sunset_new = str(datetime.datetime.strftime(sunset, "%Y-%m-%d %H:%M:%S"))tkinter.messagebox.showinfo('日落日出时间', "日出时间:" + sunrise_new + "\r" + "日落时间:" + sunset_new)# 清空输入框内容
def cancel():latitude.set('')longitude.set('')# ==============================以下代码是页面展示=========================
window = tkinter.Tk()
window.title('经纬度获取日落日出时间')# 获取屏幕尺寸计算参数,使窗口显示再屏幕中央
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
width = 400
height = 240
window_size = f'{width}x{height}+{round((screen_width-width)/2)}+{round((screen_height-height)/2)}' # round去掉小数
window.geometry(window_size)latitude = tkinter.StringVar()
longitude = tkinter.StringVar()
# 创建标签
labelLatitude = tkinter.Label(window, text='纬度', justify=tkinter.RIGHT, width=160)
# 将标签放到窗口上
labelLatitude.place(x=15, y=15, width=140, height=30)
# 创建文本框,并设置关联的变量
entryLatitude = tkinter.Entry(window, width=160, textvariable=latitude)
entryLatitude.place(x=200, y=15, width=160, height=30)labelLongitude = tkinter.Label(window, text='经度', justify=tkinter.RIGHT, width=160)
labelLongitude.place(x=5, y=60, width=160, height=30)
# 创建文本框
entryLongitude = tkinter.Entry(window, width=80, textvariable=longitude)
entryLongitude.place(x=200, y=60, width=160, height=30)# 创建日期选择器
labelDate = tkinter.Label(window, text='日期', justify=tkinter.RIGHT, width=160)
labelDate.place(x=5, y=105, width=160, height=30)
labelCal = DateEntry(window, width=12, year=datetime.datetime.now().year, month=datetime.datetime.now().month,day=datetime.datetime.now().day, background='darkblue', foreground='white', borderwidth=2)
labelCal.place(x=200, y=105, width=160, height=30)# 创建按钮组件,同时设置按钮事件处理函数
buttonOk = tkinter.Button(window, text='查询', command=sunsetQuery)
buttonOk.place(x=80, y=180, width=100, height=30)
buttonCancel = tkinter.Button(window, text='清空', command=cancel)
buttonCancel.place(x=200, y=180, width=100, height=30)
# 启动消息循环
window.mainloop()
python日出日落时间实现和详解相关推荐
- 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...
- python时间模块time详解
python时间模块time详解 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块 ...
- python利器怎么编程-Python任务调度利器之APScheduler详解
任务调度应用场景 所谓的任务调度是指安排任务的执行计划,即何时执行,怎么执行等.在现实项目中经常出现它们的身影:特别是数据类项目,比如实时统计每5分钟网站的访问量,就需要每5分钟定时从日志数据分析访问 ...
- python装饰器实例-python装饰器实例大详解
原标题:python装饰器实例大详解 一.作用域 在python中,作用域分为两种:全局作用域和局部作用域. 全局作用域是定义在文件级别的变量,函数名.而局部作用域,则是定义函数内部. 关于作用域,我 ...
- python爬虫之urllib库详解
python爬虫之urllib库详解 前言 一.urllib库是什么? 二.urllib库的使用 urllib.request模块 urllib.parse模块 利用try-except,进行超时处理 ...
- Python str / bytes / unicode 区别详解 - Python零基础入门教程
目录 一.前言 二.Python str / bytes / unicode 区别 1.Python2.x 版本中 str / bytes / unicode 区别 2.Python3.x 版本中 s ...
- python 雪花id_基于python实现雪花算法过程详解
这篇文章主要介绍了基于python实现雪花算法过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Snowflake是Twitter提出来的一个 ...
- python requests 异步调用_构建高效的python requests长连接池详解
前文: 最近在搞全网的CDN刷新系统,在性能调优时遇到了requests长连接的一个问题,以前关注过长连接太多造成浪费的问题,但因为系统都是分布式扩展的,针对这种各别问题就懒得改动了. 现在开发的缓存 ...
- python编程入门与案例详解-quot;Python小屋”免费资源汇总(截至2018年11月28日)...
原标题:"Python小屋"免费资源汇总(截至2018年11月28日) 为方便广大Python爱好者查阅和学习,特整理汇总微信公众号"Python小屋"开通29 ...
最新文章
- Go 知识点(15)— 切片长度和容量
- qpsk 锁相环_本科毕业设计课题—QPSK相干解调的MATLAB仿真(4)
- 课堂经验值管理小程序_微信小程序怎么管理门店?
- 【HBuilder】手机App推送至Apple App Store过程
- windows7系统电脑管理员权限怎么更改
- 新手学信息检索4:向量空间模型与相似度计算
- 人工神经网络----Hebbian学习规则
- python的egg包的安装和制作]
- 一段、两段及三段式状态机的写法——售货机的verilog实现
- 百度编辑器上传图片地址+上域名,让上传图片保存全路径
- 郑州java工程师待遇怎么样_Java工程师工资待遇怎么样?
- 用户体验五要素_什么是用户体验五要素?
- hyper-v开启与关闭
- 【Python 实战基础】Pandas如何统筛选复制某个数据
- 山东大学计算机考研压分,很坑!考研院校:这些骚操作,你不得不防
- 英文字母间隔很大怎么解决?全角半角的概念
- BP神经网络隐层节点的个数设置方法
- Linux基础(2)/Linux 基本概念及操作
- Windows8系统不能玩传奇的解习办法
- 智能体脂秤方案——基于4位单片机体脂秤方案设计_西城微科