本文實例為大家分享了python利用tkinter實現(xiàn)屏保的具體代碼,供大家參考,具體內容如下

import random
import tkinter
class RandomBall():
'''
運動的球
'''
def __init__(self, canvas, scrn_width,scrn_heigh):
'''
球的構造函數(shù)
:param canvas: 傳入畫布,在畫布上進行球的構造
:param scrn_width: 傳入屏幕寬度
:param scrn_heigh: 傳入屏幕高度
'''
#x,y表示出現(xiàn)的球的圓心
self.ball_x = random.randint(20, int(scrn_width - 20)) #球出現(xiàn)的隨機x坐標
self.ball_y = random.randint(10, int(scrn_heigh - 10)) #球出現(xiàn)的隨機y坐標
#模擬運動:就是不斷地重畫球,不斷地更新球的位置
self.x_move = random.randint(4, 30) #模擬x方向運動
self.y_move = random.randint(5, 20) #模擬y方向運動
#定義寬度和高度和畫布
self.canvas = canvas
self.scrn_width = scrn_width
self.scrn_heigh = scrn_heigh
#球的大小隨機
self.rad = random.randint(20, 150) #用半徑rad表示球的大小
#定義顏色
c = lambda : random.randint(0, 255)
self.color = "#%02x%02x%02x"%(c(), c(), c())
def creat_ball(self):
'''
用構造函數(shù)中的值創(chuàng)建一個球
:return:
'''
#tkinter沒有畫圓函數(shù),只有橢圓函數(shù)
#但在正方形里面畫的橢圓就是正圓
#已知圓心坐標和半徑,則圓心坐標減半徑能求出正方形左上角
#圓心坐標加上半徑,能求出右下角
#已知左上角和右上角,可以畫出
x1 = self.ball_x - self.rad #左上角的x坐標
y1 = self.ball_y - self.rad #左上角的y坐標
x2 = self.ball_x + self.rad #右下角的x坐標
y2 = self.ball_y + self.rad #右下角的y坐標
#在有對角坐標的情況下就可以創(chuàng)建圓
self.item = self.canvas.create_oval(x1, y1, x2, y2, fill = self.color, outline = self.color)
# 球動
def move_ball(self):
self.ball_x += self.x_move #球移動后的新x坐標
self.ball_y += self.y_move #球移動后的新y坐標
# 碰壁回彈判斷
if self.ball_x + self.rad >= self.scrn_width: #撞到了右邊的墻
self.x_move = -self.x_move
if self.ball_x - self.rad <= 0: #撞到了左邊的墻
self.x_move = -self.x_move
if self.ball_y + self.rad >= self.scrn_heigh: #撞到下面的墻
self.y_move = -self.y_move
if self.ball_y - self.rad <= 0: #撞到上面的墻
self.y_move = -self.y_move
self.canvas.move(self.item, self.x_move, self.y_move) #利用x,y的移動距離控制球的移動快慢
class ScreenSaver():
'''
可以被啟動的屏保
'''
#創(chuàng)建一個list裝創(chuàng)建的球
def __init__(self):
self.balls = list()
self.nums_balls = random.randint(6, 20) #產(chǎn)生隨機數(shù)量的球
self.baseFrame = tkinter.Tk() #啟動界面
self.baseFrame.overrideredirect(1) #取消邊框
#移動鼠標則退出屏保
self.baseFrame.bind("<Motion>", self.my_quit)
self.baseFrame.attributes('-alpha', 1)
#鍵盤任意鍵退出屏保
self.baseFrame.bind("<Key>",self.my_quit)
#得到屏幕的寬和高
w = self.baseFrame.winfo_screenwidth()
h = self.baseFrame.winfo_screenheight()
#創(chuàng)建畫布
self.canvas = tkinter.Canvas(self.baseFrame, width = w, height = h)
self.canvas.pack()
#在畫布上畫球
for i in range(self.nums_balls):
ball = RandomBall(self.canvas, scrn_width = w, scrn_heigh = h)
ball.creat_ball()
self.balls.append(ball)
self.run_screen_saver()
self.baseFrame.mainloop()
#球動函數(shù)
def run_screen_saver(self):
for ball in self.balls:
ball.move_ball()
#在sleep100ms以后啟動第二個參數(shù)函數(shù),相當于100ms動一次
self.canvas.after(100, self.run_screen_saver)
#當事件發(fā)生時,傳入event,退出屏保
def my_quit(self, event):
#析構(退出)屏保
self.baseFrame.destroy()
if __name__ == "__main__":
#啟動屏保
ScreenSaver()
當前標題:python利用tkinter實現(xiàn)屏保-創(chuàng)新互聯(lián)
文章出自:http://www.chinadenli.net/article28/igdcp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設計公司、企業(yè)網(wǎng)站制作、云服務器、小程序開發(fā)、關鍵詞優(yōu)化、App設計
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)