本文實例為大家分享了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('
', 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毫秒后啟動一個函數,第二個參數為需啟動的函數,類似于遞歸
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()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

