您好,登錄后才能下訂單哦!
本文實例為大家分享了python利用tkinter實現(xiàn)屏保的具體代碼,供大家參考,具體內(nèi)容如下
import random import tkinter class RandomBall(): ''' 運動的球 ''' def __init__(self, canvas, scrn_width,scrn_heigh): ''' 球的構(gòu)造函數(shù) :param canvas: 傳入畫布,在畫布上進行球的構(gòu)造 :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): ''' 用構(gòu)造函數(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): #析構(gòu)(退出)屏保 self.baseFrame.destroy() if __name__ == "__main__": #啟動屏保 ScreenSaver()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。