python tkinter库实现气泡屏保和锁屏
作者:疯牧瑾 时间:2021-09-17 22:37:57
本文实例为大家分享了python tkinter库实现气泡屏保和锁屏的具体代码,供大家参考,具体内容如下
显示效果如下:
代码:
import random
import tkinter
import threading
from ctypes import *
class RandomBall(object):
"""
定义关于球的类
"""
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)
# 将屏幕尺寸的形参赋给函数内部
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):
""" 创建球的函数"""
# 通过圆心,获取一矩形左上角和右下角的坐标
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没有创建圆的函数,通过创建椭圆的方式来生成圆
self.item = self.canvas.create_oval(x1, y1, x2, y2, fill=self.color, outline=self.color)
def move_ball(self):
"""创建球移动的函数"""
# 球的(x,y)坐标根据速度变化不断更新
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 = []
# 每次启动程序,球的数量随机
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)
# 调整背景透明度
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)
# 创建画布,包括画布的归属、尺寸和背景颜色
self.canvas = tkinter.Canvas(self.root, width=self.width, height=self.height, bg="black")
self.canvas.pack()
# 根据num_balls随机生成的数值,在画布上生成球
for i in range(self.num_balls):
# 调用RandomBall函数,自动初始化出不同大小、位置和颜色的球
ball = RandomBall(self.canvas, screen_width=self.width, screen_height=self.height)
# 调用生成球的函数
ball.create_ball()
self.balls.append(ball)
self.run_screen_saver()
self.root.mainloop()
def run_screen_saver(self):
"""调动球运动的函数"""
for ball in self.balls:
ball.move_ball()
# after函数是每200毫秒后启动一个函数,第二个参数为需启动的函数,类似于递归
self.canvas.after(50, self.run_screen_saver)
def my_quit(self, event):
"""定义一个停止运行的函数"""
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):
"""调用windll函数"""
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()
来源:https://blog.csdn.net/qq_42029527/article/details/84944890
标签:python,tkinter,屏保,锁屏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python求绝对值的三种方法小结
2023-04-17 19:57:09
![](https://img.aspxhome.com/file/2023/0/72820_0s.jpg)
如何避免查询调查结果时出现不相关主题的重复记录?
2009-11-07 18:42:00
django缓存配置的几种方法详解
2022-08-29 02:37:16
在ASP.NET 2.0中操作数据之二十七:创建自定义排序用户界面
2023-06-25 21:01:05
![](https://img.aspxhome.com/file/2023/0/76350_0s.png)
国内ASP开源建站系统一览
2009-07-10 13:21:00
优化你的ASP程序及优化网页
2007-10-06 23:02:00
Python中利用原始套接字进行网络编程的示例
2023-06-22 18:02:03
ACCESS的参数化查询 附ASP和C#(ASP.NET)函数
2008-01-10 12:18:00
pygame实现弹球游戏
2023-05-26 20:38:44
http通过StreamingHttpResponse完成连续的数据传输长链接方式
2023-06-04 03:33:45
深入浅出SQL之左连接、右连接和全连接
2009-08-30 15:14:00
使用Python脚本将绝对url替换为相对url的教程
2022-09-03 09:36:39
Dreamweaver处理word文档有妙招
2008-05-23 13:00:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113546322s.jpg)
Python中22个万用公式的小结
2022-12-15 21:06:38
![](https://img.aspxhome.com/file/2023/6/70906_0s.png)
SQL Server与Oracle、DB2的优劣对比
2009-01-07 14:16:00
使用PHP生成二维码的两种方法(带logo图像)
2023-11-14 11:00:30
Python基于pillow库实现生成图片水印
2021-08-01 10:45:38
python3 pillow生成简单验证码图片的示例
2022-08-16 23:17:05
IE8 CSS之生成内容
2008-09-09 22:14:00
FCKeditor 编辑器实战技巧 Ⅰ
2008-10-08 10:22:00
![](https://img.aspxhome.com/file/uploadpic/20071/200713110656772.gif)