本文實(shí)例為大家分享了python tkinter庫實(shí)現(xiàn)氣泡屏保和鎖屏的具體代碼,供大家參考,具體內(nèi)容如下
顯示效果如下:
代碼:?
import random import tkinter import threading from ctypes import * class RandomBall(object): """ 定義關(guān)于球的類 """ def __init__(self, canvas, screen_width, screen_height): """初始化畫布和屏幕尺寸""" self.item = None self.canvas = canvas # 定義球的初始位置(x,y),此坐標(biāo)為球的圓心,位置隨機(jī)生成 self.x_pos = random.randint(10, int(screen_width) - 20) self.y_pos = random.randint(10, int(screen_height) - 20) # 定義球在x、y方向上的移動(dòng)速度,速度隨機(jī)給定 self.x_velocity = random.randint(6, 12) self.y_velocity = random.randint(6, 12) # 將屏幕尺寸的形參賦給函數(shù)內(nèi)部 self.screen_width = screen_width self.screen_height = screen_height # 定義球的半徑,半徑大小隨機(jī)給定 self.radius = random.randint(40, 70) # 定義球的顏色 c = lambda: random.randint(0, 255) self.color = '#%02x%02x%02x' % (c(), c(), c()) def create_ball(self): """ 創(chuàng)建球的函數(shù)""" # 通過圓心,獲取一矩形左上角和右下角的坐標(biāo) x1 = self.x_pos - self.radius y1 = self.y_pos - self.radius x2 = self.x_pos + self.radius y2 = self.y_pos + self.radius # tkinter沒有創(chuàng)建圓的函數(shù),通過創(chuàng)建橢圓的方式來生成圓 self.item = self.canvas.create_oval(x1, y1, x2, y2, fill=self.color, outline=self.color) def move_ball(self): """創(chuàng)建球移動(dòng)的函數(shù)""" # 球的(x,y)坐標(biāo)根據(jù)速度變化不斷更新 self.x_pos += self.x_velocity self.y_pos += self.y_velocity # 當(dāng)球撞到屏幕邊界后,反彈的算法判斷 if self.x_pos + self.radius >= self.screen_width: self.x_velocity = -self.x_velocity if self.x_pos - self.radius <= 0: self.x_velocity = -self.x_velocity if self.y_pos + self.radius >= self.screen_height: self.y_velocity = -self.y_velocity if self.y_pos - self.radius <= 0: self.y_velocity = -self.y_velocity # 在畫布上移動(dòng)圖畫 self.canvas.move(self.item, self.x_velocity, self.y_velocity) class ScreenSaver(object): """ 定義屏保的類 """ def __init__(self): self.balls = [] # 每次啟動(dòng)程序,球的數(shù)量隨機(jī) self.num_balls = random.randint(20, 60) # 生成root主窗口 self.root = tkinter.Tk() # 獲取屏幕尺寸,作為主窗口尺寸 self.width = self.root.winfo_screenwidth() self.height = self.root.winfo_screenheight() # 取消邊框 self.root.overrideredirect(1) # 調(diào)整背景透明度 self.root.attributes('-alpha', 1) # 點(diǎn)擊鼠標(biāo)、移動(dòng)鼠標(biāo)、敲擊鍵盤時(shí)退出程序 # self.root.bind('', self.my_quit) # self.root.bind(' ', self.my_quit) self.root.bind(' ', self.my_quit) # 創(chuàng)建畫布,包括畫布的歸屬、尺寸和背景顏色 self.canvas = tkinter.Canvas(self.root, width=self.width, height=self.height, bg="black") self.canvas.pack() # 根據(jù)num_balls隨機(jī)生成的數(shù)值,在畫布上生成球 for i in range(self.num_balls): # 調(diào)用RandomBall函數(shù),自動(dòng)初始化出不同大小、位置和顏色的球 ball = RandomBall(self.canvas, screen_width=self.width, screen_height=self.height) # 調(diào)用生成球的函數(shù) ball.create_ball() self.balls.append(ball) self.run_screen_saver() self.root.mainloop() def run_screen_saver(self): """調(diào)動(dòng)球運(yùn)動(dòng)的函數(shù)""" for ball in self.balls: ball.move_ball() # after函數(shù)是每200毫秒后啟動(dòng)一個(gè)函數(shù),第二個(gè)參數(shù)為需啟動(dòng)的函數(shù),類似于遞歸 self.canvas.after(50, self.run_screen_saver) def my_quit(self, event): """定義一個(gè)停止運(yùn)行的函數(shù)""" self.root.destroy() print(event) class LockScreen(object): """定義鎖屏的類""" def __init__(self): self.HWND_BROADCAST = 0xffff self.WM_SYS_COMMAND = 0x0112 self.SC_MONITOR_POWER = 0xF170 self.MonitorPowerOff = 2 self.SW_SHOW = 5 def win_dll(self): """調(diào)用windll函數(shù)""" windll.user32.PostMessageW(self.HWND_BROADCAST, self.WM_SYS_COMMAND, self.SC_MONITOR_POWER, self.MonitorPowerOff) shell32 = windll.LoadLibrary("shell32.dll") shell32.ShellExecuteW(None, 'open', 'rundll32.exe', 'USER32,LockWorkStation', '', self.SW_SHOW) if __name__ == '__main__': ScreenSaver() t = threading.Thread(target=LockScreen().win_dll()) t.start()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
