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

yipeiwu_com7年前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实现的银行系统模拟程序完整案例

本文实例讲述了Python实现的银行系统模拟程序。分享给大家供大家参考,具体如下: 银行系统模拟程序 1、概述 ​ 使用面向对象思想模拟一个简单的银行系统,具备的功能:管理员...

Python 3.x 判断 dict 是否包含某键值的实例讲解

查询资料得 Python 可以使用两种方式判断字典是否包含某键值 1、(dict.has_key('keyname')) 2、('keyname' in dict) 觉得第二种方式太过丑...

python中计算一个列表中连续相同的元素个数方法

最简单的例子: a = [1,1,1,1,2,2,2,3,3,1,1,1,3] # 问:计算a中最多有几个连续的1 很明显,答案是4 如果用代码实现,最先想到的就是itertool...

Python实现求两个数组交集的方法示例

本文实例讲述了Python实现求两个数组交集的方法。分享给大家供大家参考,具体如下: 一、题目 给定两个数组,编写一个函数来计算它们的交集。 例1: 输入: nums1 = [1,2,...

Python3计算三角形的面积代码

关于Python语言,众说纷纭,但无外乎两种,强大,垃圾。大多数人还是对Python持肯定意见,认为它很强大。前些天和两个的大学同学聊天,一个是在做手机测试,一个是给银行系统做维护一类的...