python tkinter庫實(shí)現(xiàn)氣泡屏保和鎖屏
本文實(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('<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隨機(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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python編程新標(biāo)準(zhǔn)學(xué)會(huì)十項(xiàng)好習(xí)慣提升編碼質(zhì)量
這篇文章主要為大家介紹了Python編程新標(biāo)準(zhǔn)學(xué)會(huì)十項(xiàng)好習(xí)慣提升編碼質(zhì)量,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Python報(bào)錯(cuò)no?module?named?torch的幾種原因及解決方案
這篇文章主要給大家介紹了關(guān)于Python報(bào)錯(cuò)no?module?named?torch的幾種原因及解決方案,這是小白時(shí)常犯的錯(cuò),這個(gè)報(bào)錯(cuò)一般說明在你電腦當(dāng)前環(huán)境下沒有安裝torch這個(gè)模塊,但也有其他情況,需要的朋友可以參考下2023-10-10
python實(shí)現(xiàn)兩個(gè)dict合并與計(jì)算操作示例
這篇文章主要介紹了python實(shí)現(xiàn)兩個(gè)dict合并與計(jì)算操作,結(jié)合具體實(shí)例形式分析了Python使用collections.Counter進(jìn)行字典dict合并與遍歷輸出相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
在pycharm中為項(xiàng)目導(dǎo)入anacodna環(huán)境的操作方法
這篇文章主要介紹了在pycharm中為項(xiàng)目導(dǎo)入anacodna環(huán)境的操作方法,本文圖文并茂通過實(shí)例詳解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Python中*args與**kwargs的高級(jí)應(yīng)用指南
在Python編程中,*args和**kwargs是兩個(gè)非常強(qiáng)大的功能,它們?cè)试S開發(fā)者構(gòu)建更加靈活和可擴(kuò)展的函數(shù),下面就跟隨小編一起來看看它的具體應(yīng)用吧2024-03-03

