how to create beautiful moving stars/snow like screen saver using python and tkinter - tutorial - two | python gui example

Namaste Friends,



                                  Today I am going to show you how to create a simple moving small white dot/stars/snow screensaver using python and Tkinter?.



Tkinter module is a Standard GUI  library and this module comes with python. so, you don't need to install this package separately.


Tkinter is very easy to use but also a very powerful library.

and

In this script, we are going to use simple python programming. really basic programming of Tkinter and python.

yeah! so, if you are a beginner then this tutorial is created for you!.



with this tutorial, 


                  you will learn how to use simple python functions and modules to create a beautiful and interesting stuff.


so, let's take a look at python codes.

here are my python codes.

friends, for better understanding I am using lots of comments. only for you. So, Keep Reading and Pay Attention to codes.

1. Python Tkinter Snow Ball Screen Saver


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python

# ---------------- READ ME ---------------------------------------------
# This Script is Created Only For Practise And Educational Purpose Only
# This is an Example Of Tkinter Canvas Graphics
# This Script Is Created For http://bitforestinfo.blogspot.in
# This Script is Written By
#
#
##################################################
######## Please Don't Remove Author Name #########
############### Thanks ###########################
##################################################
#
#
__author__='''

######################################################
                By S.S.B Group                          
######################################################

    Suraj Singh
    Admin
    S.S.B Group
    surajsinghbisht054@gmail.com
    http://bitforestinfo.blogspot.in/

    Note: We Feel Proud To Be Indian
######################################################
'''
print __author__
# ============ CONFIGURATIONS =====================
NUMBER_OF_STARS = 80
RADIUS = 10
STAR_COLOR = "white"
UNIVERS_COLOR = "black"
TRANSPARENCY_LEVEL = 1
SCREEN_SAVER_CLOSING_EVENTS = ['<Any-KeyPress>', '<Any-Button>']

SPEED_GEARS = [i/10.0 for i in range(-10,-2)]+[i/10.0 for i in range(2,10)]

# =====================================================

# import module
import Tkinter
import time
import random


# Canvas class
class Sky(Tkinter.Canvas):
 def __init__(self, *args, **kwargs):
  Tkinter.Canvas.__init__(self, *args, **kwargs)
  self.stars=[]
  self.create_stars()

  # Function For Creating Stars
 def create_stars(self):
  for i in range(NUMBER_OF_STARS):
   self.stars.append(Stars(self))
  return

  # Function For Updating Stars Coordinates
 def update_screen(self):
  for i in self.stars:
   i.update()
  return
# 
#
# Stars Class Object
class Stars:
 def __init__(self, parent):
  self.parent = parent
  self.moving_start()
  self.create_small_circle()
 
 # Setup Stars Functions 
 def moving_start(self):
  self.x1 = self.parent.winfo_width()/2
  self.y1 = self.parent.winfo_height()/2
  self.velocity_x = random.choice(SPEED_GEARS)
  self.velocity_y = random.choice(SPEED_GEARS)
  return

 # Move Circle
 def moving_stop(self):
  self.parent.coords(self.star, self.x1,self.y1,self.x1+RADIUS,self.y1+RADIUS)
  self.moving_start()
  return

 # Create Small Circle
 def create_small_circle(self):
  x1=self.x1#self.parent.winfo_screenwidth()/2
  y1=self.y1#self.parent.winfo_screenheight()/2
  x2,y2=x1+RADIUS, y1+RADIUS
  self.star = self.parent.create_oval(x1,y1,x2,y2, fill=STAR_COLOR)
  return

 # update circle coordinates
 def update(self):
  self.parent.move(self.star, self.velocity_x, self.velocity_y)
  x,y = self.parent.coords(self.star)[:2]
  if x<0 or x>1500:
   self.moving_stop()
  elif y<0 or y>1000:
   self.moving_stop()
  return

# main function
def main():
 # create window object
 root=Tkinter.Tk()
 # create canvas 
 screen = Sky(root,bg=UNIVERS_COLOR)
 screen.pack(expand="yes",fill="both")

 # Tkinter Window Configurations
 root.wait_visibility(screen)
 root.wm_attributes('-alpha',TRANSPARENCY_LEVEL)
 root.wm_attributes("-topmost", True)
 root.overrideredirect(1)
 root.attributes('-fullscreen', True)
    # Window Exit Functions

    # Windows Destroy Function
 def out(event):
  root.destroy()
  return

    # Event Bindings
 for seq in SCREEN_SAVER_CLOSING_EVENTS:
  root.bind_all(seq, out)

 while True:
  root.update()
  root.update_idletasks()
  screen.update_screen()

# main trigger function
if __name__ == '__main__':
 main()

and
after running this codes.

i got this screen




               really very cool!



This Tutorial End Here! :)




if you like my blog. then,
don't forget to subscribe,
comment and share.

 Thanks for reading 
have a nice day

Share this

Related Posts

Previous
Next Post »