Python自動更換壁紙爬蟲與tkinter結合

Python自動更換壁紙爬蟲與tkinter結合

直接上代碼

import ctypes
import time
import requests
import os
from threading import Thread
from tkinter import Tk, Label, Button,Entry,StringVar,messagebox
# '放到AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup下把本文件後綴設為pyw 就會開機自啟'
class Bz(object):
 def __init__(self):
 self.curent_url = 'https://tenapi.cn/img/acg.php'
 self.root = Tk()
 self.root.attributes("-alpha", 0.8)
 self.root.title('壁紙切---by Alterem--->吾愛17788210295')
 self.root.geometry('218x55+1300+20')
 self.b1 = Button(self.root, text=' 換一類 ', command=self.change_type, fg='#00f235',width=8).place(x=0, y=0)
 self.b2 = Button(self.root, text='下一張>> 頻率(分)', command=self.change_next, fg='blue',width=22).place(x=63, y=0)
 self.b3 = Button(self.root, text='停止切換', command=self.stop, fg='red', width=8).place(x=0, y=30)
 self.label = Label(self.root, text='二次元',fg='#9e3dff')
 self.label.place(x=85, y=30)
 e = StringVar()
 self.input=Entry(self.root,textvariable=e,fg='#ffaf0a')
 e.set(0.1) #默認0.1分鐘 6s
 self.input.place(x=153, y=30)
 self.url_list = {1: ['影視', 'http://pic.tsmp4.net/api/yingshi/img.php'],
 2: ['隨機', 'http://lorempixel.com/1920/1080/'],
 3: ['女神', 'http://pic.tsmp4.net/api/nvsheng/img.php'],
 4: ['風景', 'http://pic.tsmp4.net/api/fengjing/img.php'],
 5: ['二次元', 'https://tenapi.cn/img/acg.php']}
 self.path = 'D:\壁紙'
 self.filepath = self.path + '/img.jpg'
 if not os.path.exists(self.path):
 os.makedirs(self.path)
 self.flag = True
 self.start = 1
 
 def stop(self):
 self.flag = False
 
 def change_type(self):
 if self.start > 5:
 self.start = 1
 list = self.url_list.get(self.start)
 curent_type = list[0]
 self.curent_url = list[1]
 self.label['text'] = curent_type
 self.start += 1
 
 def change_next(self):
 data = requests.get(url=self.curent_url).content
 with open(self.filepath, 'wb') as f:
 f.write(data)
 self.config()
 
 def get_img(self):
 try:
 self.num = eval(self.input.get()) * 60 #捕獲結束界面後的異常
 except Exception:
 pass
 try:
 data = requests.get(url=self.curent_url).content
 with open(self.filepath, 'wb') as f:
 f.write(data)
 time.sleep(self.num) # 睡眠單位秒
 self.config()
 except Exception:
 pass
 
 def config(self):
 ctypes.windll.user32.SystemParametersInfoW(20, 0, self.filepath, 0) # 設置桌面壁紙.
 
 def img(self):
 while self.flag:
 self.get_img()
 
 def run(self):
 t1 = Thread(target=self.img)
 # t1.setDaemon(True) #設置守護線程 --->是否完全關閉
 t1.start()
 self.root.mainloop()
 if self.flag:
 root = Tk()
 root.withdraw()
 messagebox.showinfo("by Alterem", "只關閉了界面! 圖片還在切換! 請進入任務管理器結束進程")
if __name__ == '__main__':
 b = Bz()
 b.run()

本文由博客一文多發平臺 https://openwrite.cn?from=article_bottom 發佈!


分享到:


相關文章: