溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在python中使用tkinter庫實現(xiàn)氣泡屏保和鎖屏

發(fā)布時間:2021-06-02 17:35:25 來源:億速云 閱讀:285 作者:Leah 欄目:開發(fā)技術

這期內(nèi)容當中小編將會給大家?guī)碛嘘P怎么在python中使用tkinter庫實現(xiàn)氣泡屏保和鎖屏,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

代碼: 

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)
  # 將屏幕尺寸的形參賦給函數(shù)內(nèi)部
  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):
  """ 創(chuàng)建球的函數(shù)"""
  # 通過圓心,獲取一矩形左上角和右下角的坐標
  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沒有創(chuàng)建圓的函數(shù),通過創(chuàng)建橢圓的方式來生成圓
  self.item = self.canvas.create_oval(x1, y1, x2, y2, fill=self.color, outline=self.color)
 
 def move_ball(self):
  """創(chuàng)建球移動的函數(shù)"""
  # 球的(x,y)坐標根據(jù)速度變化不斷更新
  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 = []
  # 每次啟動程序,球的數(shù)量隨機
  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)
  # 調(diào)整背景透明度
  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)
  # 創(chuàng)建畫布,包括畫布的歸屬、尺寸和背景顏色
  self.canvas = tkinter.Canvas(self.root, width=self.width, height=self.height, bg="black")
  self.canvas.pack()
 
  # 根據(jù)num_balls隨機生成的數(shù)值,在畫布上生成球
  for i in range(self.num_balls):
   # 調(diào)用RandomBall函數(shù),自動初始化出不同大小、位置和顏色的球
   ball = RandomBall(self.canvas, screen_width=self.width, screen_height=self.height)
   # 調(diào)用生成球的函數(shù)
   ball.create_ball()
   self.balls.append(ball)
  self.run_screen_saver()
  self.root.mainloop()
 
 def run_screen_saver(self):
  """調(diào)動球運動的函數(shù)"""
  for ball in self.balls:
   ball.move_ball()
  # after函數(shù)是每200毫秒后啟動一個函數(shù),第二個參數(shù)為需啟動的函數(shù),類似于遞歸
  self.canvas.after(50, self.run_screen_saver)
 
 def my_quit(self, event):
  """定義一個停止運行的函數(shù)"""
  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):
  """調(diào)用windll函數(shù)"""
  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中使用tkinter庫實現(xiàn)氣泡屏保和鎖屏了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI