python tkinter庫實現(xiàn)氣泡屏保和鎖屏
更新時間:2019年07月29日 17:19:17 作者:瘋牧瑾
這篇文章主要為大家詳細介紹了python tkinter庫實現(xiàn)氣泡屏保和鎖屏,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python tkinter庫實現(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),此坐標為球的圓心,位置隨機生成
self.x_pos = random.randint(10, int(screen_width) - 20)
self.y_pos = random.randint(10, int(screen_height) - 20)
# 定義球在x、y方向上的移動速度,速度隨機給定
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
# 定義球的半徑,半徑大小隨機給定
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ù)"""
# 通過圓心,獲取一矩形左上角和右下角的坐標
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)建球移動的函數(shù)"""
# 球的(x,y)坐標根據(jù)速度變化不斷更新
self.x_pos += self.x_velocity
self.y_pos += self.y_velocity
# 當球撞到屏幕邊界后,反彈的算法判斷
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
# 在畫布上移動圖畫
self.canvas.move(self.item, self.x_velocity, self.y_velocity)
class ScreenSaver(object):
"""
定義屏保的類
"""
def __init__(self):
self.balls = []
# 每次啟動程序,球的數(shù)量隨機
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)
# 點擊鼠標、移動鼠標、敲擊鍵盤時退出程序
# self.root.bind('<Motion>', self.my_quit)
# self.root.bind('<Any-Button>', self.my_quit)
self.root.bind('<Control-Shift-KeyPress-L>', 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隨機生成的數(shù)值,在畫布上生成球
for i in range(self.num_balls):
# 調(diào)用RandomBall函數(shù),自動初始化出不同大小、位置和顏色的球
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)動球運動的函數(shù)"""
for ball in self.balls:
ball.move_ball()
# after函數(shù)是每200毫秒后啟動一個函數(shù),第二個參數(shù)為需啟動的函數(shù),類似于遞歸
self.canvas.after(50, self.run_screen_saver)
def my_quit(self, event):
"""定義一個停止運行的函數(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()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python編程新標準學(xué)會十項好習(xí)慣提升編碼質(zhì)量
這篇文章主要為大家介紹了Python編程新標準學(xué)會十項好習(xí)慣提升編碼質(zhì)量,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
Python報錯no?module?named?torch的幾種原因及解決方案
這篇文章主要給大家介紹了關(guān)于Python報錯no?module?named?torch的幾種原因及解決方案,這是小白時常犯的錯,這個報錯一般說明在你電腦當前環(huán)境下沒有安裝torch這個模塊,但也有其他情況,需要的朋友可以參考下2023-10-10
在pycharm中為項目導(dǎo)入anacodna環(huán)境的操作方法
這篇文章主要介紹了在pycharm中為項目導(dǎo)入anacodna環(huán)境的操作方法,本文圖文并茂通過實例詳解的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02
Python中*args與**kwargs的高級應(yīng)用指南
在Python編程中,*args和**kwargs是兩個非常強大的功能,它們允許開發(fā)者構(gòu)建更加靈活和可擴展的函數(shù),下面就跟隨小編一起來看看它的具體應(yīng)用吧2024-03-03

