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实现的Excel文件读写类
Jul 30 Python
python 添加用户设置密码并发邮件给root用户
Jul 25 Python
python通过百度地图API获取某地址的经纬度详解
Jan 28 Python
python算法与数据结构之冒泡排序实例详解
Jun 22 Python
解决Python 异常TypeError: cannot concatenate 'str' and 'int' objects
Apr 08 Python
Python-for循环的内部机制
Jun 12 Python
django rest framework使用django-filter用法
Jul 15 Python
python实现发送带附件的邮件代码分享
Sep 22 Python
Pycharm中如何关掉python console
Oct 27 Python
pandas 操作 Excel操作总结
Mar 31 Python
Python中X[:,0]和X[:,1]的用法
May 10 Python
分享提高 Python 代码的可读性的技巧
Mar 03 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扩展函数
2006/10/09 PHP
关于php fread()使用技巧
2010/01/22 PHP
php实现比较全的数据库操作类
2015/06/18 PHP
PHP mysql事务问题实例分析
2016/01/18 PHP
PHP生成推广海报的方法分享
2018/04/22 PHP
php设计模式之适配器模式原理、用法及注意事项详解
2019/09/24 PHP
JS应用之禁止抓屏、复制、打印
2008/02/21 Javascript
clientX,pageX,offsetX,x,layerX,screenX,offsetLeft区别分析
2010/03/12 Javascript
用JS实现一个TreeMenu效果分享
2011/08/28 Javascript
JavaScript 对任意元素,自定义右键菜单的实现方法
2013/05/08 Javascript
jquery 实现密码框的显示与隐藏示例代码
2013/09/18 Javascript
jQuery队列操作方法实例
2014/06/11 Javascript
extjs 时间范围选择自动判断的实现代码
2014/06/24 Javascript
nodejs的HTML分析利器node-jquery用法浅析
2016/11/08 NodeJs
微信小程序HTTP接口请求封装的实现
2019/02/21 Javascript
js事件触发操作实例分析
2019/06/21 Javascript
webpack打包html里面img后src为“[object Module]”问题
2019/12/22 Javascript
js实现微信聊天效果
2020/08/09 Javascript
vue 单页应用和多页应用的优劣
2020/10/22 Javascript
[06:21]2014DOTA2国际邀请赛 庆祝VG首阶段领跑;B叔为挣牛排半夜整理情报
2014/07/13 DOTA
python中的一些类型转换函数小结
2013/02/10 Python
Python删除指定目录下过期文件的2个脚本分享
2014/04/10 Python
在Python下尝试多线程编程
2015/04/28 Python
python使用WMI检测windows系统信息、硬盘信息、网卡信息的方法
2015/05/15 Python
python xlsxwriter库生成图表的应用示例
2018/03/16 Python
keras的backend 设置 tensorflow,theano操作
2020/06/30 Python
Sneaker Studio捷克:购买运动鞋
2018/07/08 全球购物
Eagle Eyes Optics鹰眼光学:高性能太阳镜
2018/12/07 全球购物
建筑设计所实习生自我鉴定
2013/09/25 职场文书
求职自荐信
2013/12/14 职场文书
大学旷课检讨书
2014/01/28 职场文书
啤酒节策划方案
2014/05/28 职场文书
2014年体育教师工作总结
2014/12/03 职场文书
教师学期末个人总结
2015/02/13 职场文书
同学聚会致辞集锦
2015/07/28 职场文书
六五普法学习心得体会
2016/01/21 职场文书