[Asm] 纯文本查看 复制代码
import urllib.request
import ctypes
import wx
import requests
import time
import ssl
from requests.adapters import HTTPAdapter
import os
from PIL import Image
SPI_SETDESKWALLPAPER = 20
def set_wallpaper_from_url(image_url,textUrl):
try:
# 使用时间戳生成唯一的文件名
timestamp = int(time.time())
#判断路径是否存在,不存在则进行创建
if not os.path.exists(textUrl):
os.makedirs(textUrl)
download_path = r'{}\wallpaper_{}.jpg'.format(textUrl,timestamp)
urllib.request.urlretrieve(image_url, download_path)
# 获取下载文件的大小
file_size = None
while file_size is None or file_size != os.path.getsize(download_path):
file_size = os.path.getsize(download_path)
time.sleep(1) # 暂停1秒等待下载完成
# 转换并保存
image = Image.open(download_path)
image.convert('RGB').save(download_path, 'JPEG')
# 设置壁纸
# 打开指定注册表路径
# reg_key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, win32con.KEY_SET_VALUE)
# # 最后的参数:2拉伸,0居中,6适应,10填充,0平铺
# win32api.RegSetValueEx(reg_key, "WallpaperStyle", 0, win32con.REG_SZ, "10")
# # 最后的参数:1表示平铺,拉伸居中等都是0
# win32api.RegSetValueEx(reg_key, "TileWallpaper", 0, win32con.REG_SZ, "0")
# # 刷新桌面与设置壁纸
# win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, download_path, win32con.SPIF_SENDWININICHANGE)
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, download_path, 0)
wx.MessageBox("壁纸更换成功", "提示", wx.OK | wx.ICON_INFORMATION)
except Exception:
wx.MessageBox("获取不到图片", "错误提示", wx.OK | wx.ICON_ERROR)
class WallpaperChangerFrame(wx.Frame):
def __init__(self):
# 设置窗口大小为 400x300
wx.Frame.__init__(self, None, title="壁纸更换工具", size=(400, 300))
panel = wx.Panel(self)
#创建文本框
self.text_ctrl = wx.TextCtrl(panel, style=wx.TE_LEFT | wx.TE_PROCESS_ENTER)
self.text_ctrl.SetHint('请输入存储图片的路径{例如: D:\image }')
# 创建单选框组
choices = ["现代美女", "二次元美女", "古风美女", "Bing"]
self.radiobox = wx.RadioBox(panel, label="请选择一个选项", choices=choices, majorDimension=1, style=wx.RA_SPECIFY_ROWS)
# 设置按钮尺寸为 150x50
self.button = wx.Button(panel, label="更换壁纸", size=(150, 50))
self.button.Bind(wx.EVT_BUTTON, self.on_change_wallpaper)
#创建布局器
sizer = wx.BoxSizer(wx.VERTICAL)
# 按钮垂直居中显示
# 添加到容器中
sizer.Add(self.text_ctrl, 1, wx.EXPAND | wx.ALL, 15)
sizer.Add(self.radiobox, 0, wx.ALIGN_CENTER | wx.TOP, 40)
sizer.Add(self.button, 0, wx.ALIGN_CENTER | wx.TOP, 60)
panel.SetSizer(sizer)
def on_change_wallpaper(self, event):
# 这里假设从一个模拟的 API 获取图片链接
try:
textUrl = self.text_ctrl.GetValue()
if textUrl == "":
return wx.MessageBox("请输入存储图片的路径", "错误提示", wx.OK | wx.ICON_ERROR)
# 获取单选框选中的选项文本
selection = self.radiobox.GetStringSelection()
if selection == "现代美女":
api_url = "https://cdn.seovx.com/?mom=302"
elif selection == "二次元美女":
api_url = "https://cdn.seovx.com/d/?mom=302"
elif selection == "古风美女":
api_url = "https://cdn.seovx.com/ha/?mom=302"
elif selection == "Bing":
#高清
api_url = "https://bing.img.run/rand.php"
#标清
# api_url = "https://bing.img.run/rand_1366x768.php"
else:
return
# 创建一个SSL上下文,加载系统默认的信任库
ssl_context = ssl.create_default_context()
# 创建session对象并设置SSL上下文
session = requests.Session()
session.verify = False
session.context = ssl_context
session.mount('http://', HTTPAdapter(max_retries=3))
session.mount('https://', HTTPAdapter(max_retries=3))
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
}
response = requests.get(api_url,headers=headers, stream=True, verify=False, timeout=(5,5),allow_redirects=True)
image_url = response.url
if image_url:
set_wallpaper_from_url(image_url,textUrl)
else:
wx.MessageBox("获取不到图片", "错误提示", wx.OK | wx.ICON_ERROR)
except Exception as e:
print(e)
wx.MessageBox(f"获取图片时出现错误:{e}", "错误提示", wx.OK | wx.ICON_ERROR)
if __name__ == '__main__':
app = wx.App()
frame = WallpaperChangerFrame()
frame.Show()
app.MainLoop()