用python的kivy写了个桌面时间,本意只是想实时看下今天的时辰和天干地支,还加了个桌面置顶,就是透明度还弄不出来。因为最近研究掐指算所以自己弄了一个哈哈哈,后面看看有没有机会烧录到墨水屏里面。各位大佬有用的就自取吧。
[Python] 纯文本查看 复制代码
import datetime
import lunarcalendar
from kivy.app import App
from kivy.config import Config
# 设置窗口大小
Config.set('graphics', 'width', '380')
Config.set('graphics', 'height', '120')
Config.set('kivy', 'default_font', [
'SimSun', # 字体
'fonts\msyh.ttc' # 字体文件路径
])
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.clock import Clock
from zhdate import ZhDate
import win32gui
import win32con
import threading
from kivy.logger import Logger
Logger.disabled = True
# 使用kv文件定义界面布局
Builder.load_string('''
:
orientation: 'vertical'
padding: 10
spacing: 10
Label:
id: current_time_label
font_size: 30
color: 1, 1, 1, 1
Label:
id: lunar_date_label
font_size: 20
color: 1, 1, 1, 1
Label:
id: tiangan_dizhi_label
font_size: 20
color: 1, 1, 1, 1
''')
class DesktopClock(BoxLayout):
current_time = StringProperty('')
lunar_date = StringProperty('')
tiangan_dizhi = StringProperty('')
def __init__(self, **kwargs):
super(DesktopClock, self).__init__(**kwargs)
# 初始化属性值
self.current_time = ''
self.lunar_date = ''
self.tiangan_dizhi = ''
# 创建新线程
self.update_thread = threading.Thread(target=self.update_time_thread)
# 将线程设置为守护线程
self.update_thread.daemon = True
# 启动线程
self.update_thread.start()
def update_time_thread(self):
while True:
# 获取当前公历时间
now = datetime.datetime.now()
self.current_time = now.strftime('%Y-%m-%d %H:%M:%S')
# 计算农历时间和天干地支
lunar = lunarcalendar.Lunar(now.year, now.month, now.day)
lunar_datetime = datetime.datetime(lunar.year, lunar.month, lunar.day)
lunar_chinese = ZhDate.from_datetime(lunar_datetime).chinese()
lunar_date = lunar_chinese.split()[0] + '' + lunar_chinese.split()[2]
tiangan_dizhi = lunar_chinese.split()[1]
# 获取当前时辰
dizhi = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥']
shichen = dizhi[(now.hour+1)//2%12]
# 更新标签
self.current_time = now.strftime('%Y-%m-%d %H:%M:%S')
self.lunar_date = lunar_date
self.tiangan_dizhi = tiangan_dizhi + ' - ' + shichen + '时'
self.ids.current_time_label.text = self.current_time
self.ids.lunar_date_label.text = self.lunar_date
self.ids.tiangan_dizhi_label.text = self.tiangan_dizhi
def get_parent_window(self):
"""
获取窗口句柄
"""
# 获取窗口句柄
hwnd = win32gui.GetForegroundWindow()
return hwnd
class DesktopClockApp(App):
def build(self):
self.title = '时间工具 -- 吾爱破解'
# 创建窗口对象
desktop_clock = DesktopClock()
# 获取窗口句柄
hwnd = desktop_clock.get_parent_window()
# 将窗口置顶
win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE)
return desktop_clock
if __name__ == '__main__':
DesktopClockApp().run()
主要是通过以上代码实现的。