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

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