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设计】。

相关文章

使用pandas把某一列的字符值转换为数字的实例

今天小编就为大家分享一篇使用pandas把某一列的字符值转换为数字的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 使用map的方法就可以实现把某一列的字符类型的值...

对pandas处理json数据的方法详解

对pandas处理json数据的方法详解

今天展示一个利用pandas将json数据导入excel例子,主要利用的是pandas里的read_json函数将json数据转化为dataframe。 先拿出我要处理的json字符串:...

python网络编程之读取网站根目录实例

本文实例讲述了python网络编程之读取网站根目录的方法,分享给大家供大家参考。 具体实现方法如下: import socket, sys port = 70 host =...

python数据类型判断type与isinstance的区别实例解析

在项目中,我们会在每个接口验证客户端传过来的参数类型,如果验证不通过,返回给客户端“参数错误”错误码。 这样做不但便于调试,而且增加健壮性。因为客户端是可以作弊的,不要轻易相信客户端传过...

python实现的重启关机程序实例

本文实例讲述了Python实现的重启关机程序的方法,对Python程序设计有一定的参考价值。具体方法如下: 实例代码如下: #!/usr/bin/python #coding=utf...