python3.6 tkinter實(shí)現(xiàn)屏保小程序
本文實(shí)例為大家分享了python3.6 tkinter實(shí)現(xiàn)屏保小程序,供大家參考,具體內(nèi)容如下
該小程序是在閑著沒事的時(shí)候,隨便寫的,就當(dāng)打發(fā)無(wú)聊了。
該程序是用python3.6寫的,調(diào)用了python中的tkinter的庫(kù)(*python2x與python3x的thinter有很多不同的地方,一定要特別注意!?。。?/p>
from random import randint from tkinter import * class Randball(): def __init__(self,canvas,scrnwidth,scrnheight): #初始化畫布 self.canvas = canvas #初始化球的圓心坐標(biāo) self.x_pos = randint(50,int(scrnwidth))#X軸的坐標(biāo) randint 隨機(jī)產(chǎn)生一個(gè)范圍內(nèi)的整數(shù) self.y_pos = randint(50,int(scrnheight))#Y軸的坐標(biāo) #球的移動(dòng)距離 self.x_move = 10 self.y_move = 10 #整個(gè)屏幕的高和寬 self.scrnwidth =scrnwidth self.scrnheight =scrnheight #初始化球的半徑 self.randius = randint(10,80) #隨機(jī)產(chǎn)生球的顏色 rc = lambda : randint(0,255) self.color = '#%02x%02x%02x'%(rc(),rc(),rc()) def create_ball(self): #計(jì)算得到用于創(chuàng)建球的四個(gè)坐標(biāo) x1 = self.x_pos - self.randius y1 = self.y_pos - self.randius x2 = self.x_pos + self.randius y2 = self.y_pos + self.randius #畫球 self.item =self.canvas.create_oval(x1,y1,x2,y2,fill = self.color,outline = self.color) def move_ball(self): #球按照指定距離移動(dòng),如果碰到障礙就向相反的方向運(yùn)動(dòng) self.x_pos += self.x_move self.y_pos += self.y_move if self.x_pos >= self.scrnwidth - self.randius: self.x_move = -self.x_move elif self.y_pos >= self.scrnheight - self.randius: self.y_move = -self.y_move elif self.x_pos < self.randius: self.x_move = abs(self.x_move) elif self.y_pos < self.randius: self.y_move = abs(self.y_move) self.canvas.move(self.item,self.x_move,self.y_move) class Screensaver(): balls = [] def __init__(self,ball_nums): self.win = Tk() self.width = self.win.winfo_screenwidth() self.height = self.win.winfo_screenheight() self.win.overrideredirect(True) self.win.attributes('-alpha',1) #綁定事件,有任何動(dòng)作退出屏保 self.win.bind('<Any-Button>',self.exit_screensaver) self.win.bind('<Motion>',self.exit_screensaver ) self.canvas = Canvas(self.win,width = self.width,height = self.height,bg="#FFFFFF")#背景 顏色自己隨便調(diào)整,至于啥顏色就看自己的心情了 self.canvas.pack() for i in range(0,ball_nums): ball = Randball(self.canvas,scrnwidth=self.width,scrnheight=self.height) ball.create_ball() self.balls.append(ball) self.run_screensaver() self.win.mainloop() def run_screensaver(self): for ball in self.balls: ball.move_ball() self.canvas.after(30,self.run_screensaver) def exit_screensaver(self,event): self.win.destroy() def main(): Screensaver(30)#屏保上球的個(gè)數(shù) if __name__=='__main__': main()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python代碼實(shí)現(xiàn)找到列表中的奇偶異常項(xiàng)
這篇文章主要介紹了Python代碼實(shí)現(xiàn)找到列表中的奇偶異常項(xiàng),文章內(nèi)容主要利用Python代碼實(shí)現(xiàn)了從輸入列表中尋找奇偶異常項(xiàng),需要的朋友可以參考一下2021-11-11Python使用pptx實(shí)現(xiàn)復(fù)制頁(yè)面到其他PPT中
這篇文章主要為大家詳細(xì)介紹了python如何使用pptx庫(kù)實(shí)現(xiàn)從一個(gè)ppt復(fù)制頁(yè)面到另一個(gè)ppt里面,文中的示例代碼講解詳細(xì),感興趣的可以嘗試一下2023-02-02Python中文糾錯(cuò)的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要是用 Python 實(shí)現(xiàn)了簡(jiǎn)單的中文分詞的同音字糾錯(cuò),目前的案例中只允許錯(cuò)一個(gè)字,感興趣的小伙伴們可以參考一下2021-07-07在Python中如何傳遞任意數(shù)量的實(shí)參的示例代碼
這篇文章主要介紹了在Python中如何傳遞任意數(shù)量的實(shí)參的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03python?使用ctypes調(diào)用C/C++?dll詳情
這篇文章主要介紹了python?使用ctypes調(diào)用C/C++?dll詳情,文章首先通過(guò)導(dǎo)入ctypes模塊,加載C/C++?dll到python進(jìn)程空間展開主題相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-04-04Pandas之Fillna填充缺失數(shù)據(jù)的方法
這篇文章主要介紹了Pandas之Fillna填充缺失數(shù)據(jù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06pytest接口測(cè)試之fixture傳參數(shù)request的使用
本文主要介紹了pytest接口測(cè)試之fixture傳參數(shù)request的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08python中@contextmanager實(shí)例用法
在本篇文章里小編給大家整理的是一篇關(guān)于python中@contextmanager實(shí)例用法,對(duì)此有興趣的朋友們可以學(xué)習(xí)下。2021-02-02