python tkinter库实现气泡屏保和锁屏

yipeiwu_com6年前Python基础

本文实例为大家分享了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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python实现的远程登录windows系统功能示例

本文实例讲述了Python实现的远程登录windows系统功能。分享给大家供大家参考,具体如下: 首先安装wmi 命令: pip install wmi 然后会报错缺少pyw...

python实现局域网内实时通信代码

使用场景,本地服务器一直在运算数据,实时发送这些数据给客户端,本地局域网内其他客户,可以实时连接服务器,获取服务器数据,互不影响。 python2服务端 #-*- coding:ut...

Python给图像添加噪声具体操作

Python给图像添加噪声具体操作

在我们进行图像数据实验的时候往往需要给图像添加相应的噪声,那么该怎么添加呢,下面给出具体得操作方法。 1、打开Python的shell界面,界面如图所示; 2、载入skimage工具包...

Python3中使用PyMongo的方法详解

前言 本文主要给大家介绍的是关于在Python3使用PyMongo的方法,分享出来供大家参考学习,下面话不多说了,来一起看看详细介绍: MongoDB存储 在这里我们来看一下Python...

Python3.x中自定义比较函数

在Python3.x的世界里,cmp函数没有了。那么sorted,min,max等需要比较函数作为参数的函数该如何用呢? 以min函数的定义为例,有两种重载形式: 单参数(一个迭代器):...