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 Mysql自动备份脚本
Jul 14 Python
python 获取文件列表(或是目录例表)
Mar 25 Python
python通过装饰器检查函数参数数据类型的方法
Mar 13 Python
Python使用multiprocessing实现一个最简单的分布式作业调度系统
Mar 14 Python
python爬取拉勾网职位数据的方法
Jan 24 Python
python 不以科学计数法输出的方法
Jul 16 Python
python实现屏保计时器的示例代码
Aug 08 Python
Python数据类型之List列表实例详解
May 08 Python
Python批量生成幻影坦克图片实例代码
Jun 04 Python
Python字符串处理的8招秘籍(小结)
Aug 13 Python
python实现12306登录并保存cookie的方法示例
Dec 17 Python
Python3如何使用range函数替代xrange函数
Oct 05 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
php桌面中心(四) 数据显示
2007/03/11 PHP
一些PHP Coding Tips(php小技巧)[2011/04/02最后更新]
2011/05/02 PHP
php图像处理函数大全(推荐收藏)
2013/07/11 PHP
PHP开发APP端微信支付功能
2017/02/17 PHP
CI(CodeIgniter)框架视图中加载视图的方法
2017/03/24 PHP
PHP实现微信支付(jsapi支付)流程步骤详解
2018/03/15 PHP
详解关于php的xdebug配置(编辑器vscode)
2019/01/29 PHP
DB.ASP 用Javascript写ASP很灵活很好用很easy
2011/07/31 Javascript
如何使用json在前后台进行数据传输实例介绍
2013/04/11 Javascript
JS获取网页属性包括宽、高等等
2014/04/03 Javascript
JavaScript获取一个范围内日期的方法
2015/04/24 Javascript
JQuery判断checkbox是否选中及其它复选框操作方法合集
2015/06/01 Javascript
Bootstrap每天必学之基础排版
2015/11/20 Javascript
使用jQuery.form.js/springmvc框架实现文件上传功能
2016/05/12 Javascript
JavaScript中的Object对象学习教程
2016/05/20 Javascript
详解JavaScript中this关键字的用法
2016/05/26 Javascript
JS实现同一DOM元素上onClick事件与onDblClick事件并存的解决方法
2018/06/07 Javascript
vue项目打包之开发环境和部署环境的实现
2020/04/23 Javascript
JS变量提升及函数提升实例解析
2020/09/03 Javascript
[00:56]2014DOTA2国际邀请赛 DK、iG 赛前探访
2014/07/10 DOTA
Python使用MYSQLDB实现从数据库中导出XML文件的方法
2015/05/11 Python
Django基于ORM操作数据库的方法详解
2018/03/27 Python
pandas按若干个列的组合条件筛选数据的方法
2018/04/11 Python
对numpy数据写入文件的方法讲解
2018/07/09 Python
pytorch 固定部分参数训练的方法
2019/08/17 Python
Python计算信息熵实例
2020/06/18 Python
哪种Python框架适合你?简单介绍几种主流Python框架
2020/08/04 Python
详解Scrapy Redis入门实战
2020/11/18 Python
eBay澳大利亚站:eBay.com.au
2018/02/02 全球购物
美国肌肉和力量商店:Muscle & Strength
2019/06/22 全球购物
英语感恩演讲稿
2014/01/14 职场文书
办公室主任个人对照检查材料思想汇报
2014/10/11 职场文书
2014年电工工作总结
2014/11/20 职场文书
毕业论文致谢部分怎么写
2015/05/14 职场文书
网聊搭讪开场白
2015/05/28 职场文书
关于公司年会的开幕词
2016/03/04 职场文书