首页 > 编程 > Python > 正文

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

2019-11-25 12:10:47
字体:
来源:转载
供稿:网友

本文实例为大家分享了python tkinter库实现气泡屏保和锁屏的具体代码,供大家参考,具体内容如下

显示效果如下:

代码: 

import randomimport tkinterimport threadingfrom 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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表