python tkinter实现屏保程序


Posted in Python onJuly 30, 2019

本文实例为大家分享了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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现支持目录FTP上传下载文件的方法
Jun 03 Python
python实现搜索本地文件信息写入文件的方法
Feb 22 Python
Python基于回溯法子集树模板解决旅行商问题(TSP)实例
Sep 05 Python
解决python 无法加载downsample模型的问题
Oct 25 Python
在Python中构建增广矩阵的实现方法
Jul 01 Python
python爬虫 基于requests模块的get请求实现详解
Aug 20 Python
python输出带颜色字体实例方法
Sep 01 Python
jupyter note 实现将数据保存为word
Apr 14 Python
Keras实现支持masking的Flatten层代码
Jun 16 Python
python3将变量输入的简单实例
Aug 19 Python
python 利用jieba.analyse进行 关键词提取
Dec 17 Python
Python Django获取URL中的数据详解
Nov 01 Python
python pandas 时间日期的处理实现
Jul 30 #Python
Django 反向生成url实例详解
Jul 30 #Python
Python Pandas数据中对时间的操作
Jul 30 #Python
python tkinter实现彩球碰撞屏保
Jul 30 #Python
详解python pandas 分组统计的方法
Jul 30 #Python
python文档字符串(函数使用说明)使用详解
Jul 30 #Python
python3.6 tkinter实现屏保小程序
Jul 30 #Python
You might like
使用ThinkPHP自带的Http类下载远程图片到本地的实现代码
2011/08/02 PHP
php去除字符串换行符示例分享
2014/02/13 PHP
PHP简单实现断点续传下载的方法
2015/09/25 PHP
php进程(线程)通信基础之System V共享内存简单实例分析
2019/11/09 PHP
仿服务器端脚本方式的JS模板实现方法
2007/04/27 Javascript
juqery 学习之三 选择器 可见性 元素属性
2010/11/25 Javascript
jQuery+.net实现浏览更多内容(改编php版本)
2013/03/28 Javascript
js格式化时间小结
2014/11/03 Javascript
Vue服务端渲染和Vue浏览器端渲染的性能对比(实例PK )
2017/03/31 Javascript
详解JS中遍历语法的比较
2017/04/07 Javascript
基于nodejs 的多页面爬虫实例代码
2017/05/31 NodeJs
JS实现轮播图效果
2020/01/11 Javascript
[48:00]EG vs LGD 2018国际邀请赛淘汰赛BO3 第二场 8.26
2018/08/29 DOTA
[03:02]2020完美世界城市挑战赛(秋季赛)总决赛回顾
2021/03/11 DOTA
python实现调用其他python脚本的方法
2014/10/05 Python
浅析Python中将单词首字母大写的capitalize()方法
2015/05/18 Python
Python彩色化Linux的命令行终端界面的代码实例分享
2016/07/02 Python
PyTorch线性回归和逻辑回归实战示例
2018/05/22 Python
基于python的图片修复程序(实现水印去除)
2018/06/04 Python
Python脚本按照当前日期创建多级目录
2019/03/01 Python
django2笔记之路由path语法的实现
2019/07/17 Python
使用openCV去除文字中乱入的线条实例
2020/06/02 Python
.img/.hdr格式转.nii格式的操作
2020/07/01 Python
CSS类名支持中文命名的示例
2014/04/04 HTML / CSS
利用CSS3实现圆角的outline效果的教程
2015/06/05 HTML / CSS
HolidayLettings英国:预订最好的度假公寓、别墅和自助式住宿
2019/08/27 全球购物
Booking.com亚太地区:Booking.com APAC
2020/02/07 全球购物
致标枪运动员加油稿
2014/02/15 职场文书
工作迟到检讨书
2014/02/21 职场文书
安全生产网格化管理实施方案
2014/03/01 职场文书
抽奖活动主持词
2014/03/31 职场文书
法学求职信
2014/06/22 职场文书
安全责任书模板
2014/07/22 职场文书
2014年教研员工作总结
2014/12/23 职场文书
2015年春训学习心得体会范文
2015/03/09 职场文书
2015年大学班级工作总结
2015/04/28 职场文书