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

  • 时间:
  • 浏览:18

本文真例为各人分享了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):
  """ 创立球的函数"""
  # 经由过程圆心,获得1矩形左上角战左下角的坐标
  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('', self.my_quit)
  # self.root.bind('', self.my_quit)
  self.root.bind('', 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毫秒后启动1个函数,第2个参数为需启动的函数,相似于递回
  self.canvas.after(50, self.run_screen_saver)
 
 def my_quit(self, event):
  """界说1个截至运转的函数"""
  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()

以上便是本文的全数内容,期望对各人的进修有所帮忙,也期望各人多多撑持剧本之家。