python tkinter实现屏保程序
作者:wjcaiyf 时间:2023-02-20 14:31:20
本文实例为大家分享了python tkinter实现屏保程序的具体代码,供大家参考,具体内容如下
该脚本摘录自: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)
来源:https://blog.csdn.net/wjciayf/article/details/50679746
标签:python,tkinter,屏保
0
投稿
猜你喜欢
js实时获得服务器上时间
2008-11-25 13:55:00
asp如何修改WINNT的登录密码?
2010-06-10 17:06:00
Go语言使用sort包对任意类型元素的集合进行排序的方法
2023-09-02 03:55:18
js中int和string数据类型互相转化实例
2024-05-02 17:25:57
利用XMLHTTP检测网址及探测服务器类型
2009-04-24 15:12:00
百度地图API之本地搜索与范围搜索
2023-08-23 17:24:38
Python插件机制实现详解
2021-08-28 06:55:30
Python实现随机生成迷宫并自动寻路
2023-11-18 11:12:41
python3 批量获取对应端口服务的实例
2021-07-14 11:52:05
python字典的元素访问实例详解
2023-06-10 10:54:11
PHP chunk_split()函数讲解
2023-06-09 13:54:44
mysql 获取规定时间段内的统计数据
2024-01-24 11:25:10
tensorflow之读取jpg图像长和宽实例
2023-10-12 11:57:41
在Windows系统上安装Cygwin搭建Swoole测试环境的图文教程
2022-11-13 21:19:34
基于Python实现快递信息提取
2022-05-02 13:41:11
99%的程序员都会收藏的书单 你读过几本?
2022-10-01 19:45:59
为什么Python中没有"a++"这种写法
2023-12-04 09:40:57
python网络爬虫实战
2021-04-16 13:31:07
在ASP.NET 2.0中操作数据之二十二:为删除数据添加客户端确认
2024-05-09 09:03:54
python计算一个序列的平均值的方法
2023-08-25 06:40:17