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中使用urllib2伪造HTTP报头的2个方法
Jul 07 Python
跟老齐学Python之玩转字符串(2)更新篇
Sep 28 Python
ubuntu系统下 python链接mysql数据库的方法
Jan 09 Python
Python matplotlib绘图可视化知识点整理(小结)
Mar 16 Python
pandas 数据实现行间计算的方法
Jun 08 Python
windows下 兼容Python2和Python3的解决方法
Dec 05 Python
Python设计模式之外观模式实例详解
Jan 17 Python
Django框架用户注销功能实现方法分析
May 28 Python
Pytorch中的variable, tensor与numpy相互转化的方法
Oct 10 Python
TensorFlow——Checkpoint为模型添加检查点的实例
Jan 21 Python
使用Python3 poplib模块删除服务器多天前的邮件实现代码
Apr 24 Python
Python实现定时监测网站运行状态的示例代码
Sep 30 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登陆后跳转到登陆前页面实现思路及代码
2014/01/17 PHP
ie focus bug 解决方法
2009/09/03 Javascript
$.ajax返回的JSON无法执行success的解决方法
2011/09/09 Javascript
Web Inspector:关于在 Sublime Text 中调试Js的介绍
2013/04/18 Javascript
js内存泄露的几种情况详细探讨
2013/05/31 Javascript
sencha touch 模仿tabpanel导航栏TabBar的实例代码
2013/10/24 Javascript
ExtJS4利根据登录后不同的角色分配不同的树形菜单
2014/05/02 Javascript
浅谈JavaScript Date日期和时间对象
2014/12/29 Javascript
jquery插件star-rating.js实现星级评分特效
2015/04/15 Javascript
jQuery弹簧插件编写基础之“又见弹窗”
2015/12/11 Javascript
详解Document.Cookie
2015/12/25 Javascript
基于JS实现新闻列表无缝向上滚动实例代码
2016/01/22 Javascript
浅谈jQuery添加的HTML,JS失效的问题
2016/10/05 Javascript
JavaScript自动点击链接 防止绕过浏览器访问的方法
2017/01/19 Javascript
vue、react等单页面项目应该这样子部署到服务器
2018/01/03 Javascript
微信小程序实现导航栏选项卡效果
2020/06/19 Javascript
vue实现抽屉弹窗效果
2020/11/15 Javascript
Vue包大小优化的实现(从1.72M到94K)
2021/02/18 Vue.js
[02:01]大师之路——DOTA2完美大师赛11月论剑上海
2017/11/06 DOTA
Python爬取读者并制作成PDF
2015/03/10 Python
Python统计日志中每个IP出现次数的方法
2015/07/06 Python
Python selenium如何设置等待时间
2016/09/15 Python
python3设计模式之简单工厂模式
2017/10/17 Python
python绘制封闭多边形教程
2020/02/18 Python
windows下Pycharm安装opencv的多种方法
2020/03/05 Python
Python实现发票自动校核微信机器人的方法
2020/05/22 Python
python 如何快速复制序列
2020/09/07 Python
StubHub德国:购买和出售门票
2017/09/06 全球购物
维也纳通行证:Vienna PASS
2019/07/18 全球购物
Wolford法国官网:奥地利奢侈内衣品牌
2020/08/11 全球购物
软件测试工程师面试问题精选
2016/10/28 面试题
教师实习自我鉴定
2013/12/14 职场文书
2014年师德师风学习材料
2014/05/16 职场文书
2014年干部培训工作总结
2014/12/17 职场文书
爱国主义教育主题班会
2015/08/13 职场文书
Python中三种花式打印的示例详解
2022/03/19 Python