python tkinter實(shí)現(xiàn)屏保程序
本文實(shí)例為大家分享了python tkinter實(shí)現(xiàn)屏保程序的具體代碼,供大家參考,具體內(nèi)容如下
該腳本摘錄自:2014年辛星tkinter教程第二版
#!/usr/bin/env python
from Tkinter import *
from random import randint
class RandomBall(object):
def __init__(self, canvas, screenwidth, screenheight):
self.canvas = canvas
self.xpos = randint(10, int(screenwidth))
self.ypos = randint(10, int(screenheight))
self.xspeed = randint(6, 12)
self.yspeed = randint(6, 12)
self.screenwidth = screenwidth
self.screenheight = screenheight
self.radius = randint(40, 70)
color = lambda : randint(0, 255)
self.color = '#%02x%02x%02x' % (color(), color(), color())
def create_ball(self):
x1 = self.xpos - self.radius
y1 = self.ypos - self.radius
x2 = self.xpos + self.radius
y2 = self.ypos + self.radius
self.itm = self.canvas.create_oval(x1, y1, x2, y2, fill=self.color,
outline=self.color)
def move_ball(self):
self.xpos += self.xspeed
self.ypos += self.yspeed
if self.ypos >= self.screenheight - self.radius:
self.yspeed = -self.yspeed
if self.ypos <= self.radius:
self.yspeed = abs(self.yspeed)
if self.xpos >= self.screenwidth - self.radius or self.xpos <= self.radius:
self.xspeed = -self.xspeed
self.canvas.move(self.itm, self.xspeed, self.yspeed)
class ScreenSaver:
def __init__(self, num_balls):
self.balls = []
self.root = Tk()
w, h = self.root.winfo_screenwidth(), self.root.winfo_screenheight()
self.root.overrideredirect(1)
self.root.attributes('-alpha', 0.3)
self.root.bind('<Key>', self.myquit)
self.root.bind('<Motion>', self.myquit)
self.canvas = Canvas(self.root, width=w, height=h)
self.canvas.pack()
for i in range(num_balls):
ball = RandomBall(self.canvas, screenwidth=w, screenheight=h)
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()
self.canvas.after(50, self.run_screen_saver)
def myquit(self, event):
self.root.destroy()
if __name__ == "__main__":
ScreenSaver(18)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Django框架中數(shù)據(jù)的連鎖查詢(xún)和限制返回?cái)?shù)據(jù)的方法
這篇文章主要介紹了Django框架中數(shù)據(jù)的連鎖查詢(xún)和限制返回?cái)?shù)據(jù)的方法,Django是Python重多高人氣框架中最為著名的一個(gè),需要的朋友可以參考下2015-07-07
django 利用Q對(duì)象與F對(duì)象進(jìn)行查詢(xún)的實(shí)現(xiàn)
這篇文章主要介紹了django 利用Q對(duì)象與F對(duì)象進(jìn)行查詢(xún)的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
python中常見(jiàn)的幾種音頻數(shù)據(jù)讀取、保存方式總結(jié)
Python是一種非常適合進(jìn)行音頻處理和音頻分析的語(yǔ)言,因?yàn)樗性S多強(qiáng)大的庫(kù)可以使用,下面這篇文章主要給大家介紹了關(guān)于python中常見(jiàn)的幾種音頻數(shù)據(jù)讀取、保存方式,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
python使用pandas讀取json文件并進(jìn)行刷選導(dǎo)出xlsx文件的方法示例
這篇文章主要介紹了python使用pandas讀取json文件并進(jìn)行刷選導(dǎo)出xlsx文件的方法,結(jié)合實(shí)例形式分析了python調(diào)用pandas模塊針對(duì)json數(shù)據(jù)操作的相關(guān)使用技巧,需要的朋友可以參考下2023-06-06
Python批量啟動(dòng)多線(xiàn)程代碼實(shí)例
這篇文章主要介紹了python批量啟動(dòng)多線(xiàn)程代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
python二維鍵值數(shù)組生成轉(zhuǎn)json的例子
今天小編就為大家分享一篇python二維鍵值數(shù)組生成轉(zhuǎn)json的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
pandas的連接函數(shù)concat()函數(shù)的具體使用方法
這篇文章主要介紹了pandas的連接函數(shù)concat()函數(shù)的具體使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

