Tkinter XMas Graphics  | Expert | | Join Date: Jul 2006 Location: Norway
Posts: 112
| | -
import Tkinter
-
import random
-
-
root = Tkinter.Tk()
-
w = Tkinter.Canvas(root, width=400, height=300, background="#000000")
-
w.create_text(200,150,text="Happy Christmas 2006",font="Arial 20",fill="#ff0000")
-
w.create_text(200,170,text="from kudos",font="Arial 12",fill="#00ff00")
-
w.pack()
-
-
flake = [];
-
moves = []
-
for i in range(50):
-
flake.append(w.create_text(random.randrange(400),random.randrange(300),text="*",fill="#ffffff",font="Times 30"))
-
moves.append([0.04 + random.random()/10,0.7 + random.random()])
-
try:
-
while 1:
-
for i in range(len(flake)):
-
p = w.coords(flake[i])
-
p[0]+=moves[i][0]
-
p[1]+=moves[i][1]
-
w.coords(flake[i],p[0],p[1])
-
if(p[1]>310):
-
w.coords(flake[i],random.randrange(400),-10)
-
root.update_idletasks() # redraw
-
root.update() # process events
-
except:
-
pass
-
run it on your computer..
-best wishes kudos
|  | Moderator | | Join Date: Sep 2006 Location: Minden, Nevada, USA
Posts: 6,400
| | | re: Tkinter XMas Graphics
Very cool! Thanks kudos. Happy Christmas to you, too.
Nice, elegant piece of software, my friend. Keep posting,
Barton
|  | Expert | | Join Date: Jul 2006 Location: Norway
Posts: 112
| | | re: Tkinter XMas Graphics
Hi,
yeah true, it would be a problem if the try except would be inside the while loop, but since it outside, I guess the only Tclerror you going to get is when the window is closed. But I really haven't used Tcl much, not even when programming applications on IRIX and Solaris (used Xlib) -
import Tkinter
-
import random
-
-
root = Tkinter.Tk()
-
w = Tkinter.Canvas(root, width=400, height=300, background="#000000")
-
w.create_text(200,150,text="Happy Christmas 2006",font="Arial 20",fill="#ff0000")
-
w.create_text(200,170,text="from kudos",font="Arial 12",fill="#00ff00")
-
w.pack()
-
-
flake = [];
-
moves = []
-
for i in range(50):
-
flake.append(w.create_text(random.randrange(400),random.randrange(300),text="*",fill="#ffffff",font="Times 30"))
-
moves.append([0.04 + random.random()/10,0.7 + random.random()])
-
try:
-
while 1:
-
for i in range(len(flake)):
-
p = w.coords(flake[i])
-
p[0]+=moves[i][0]
-
p[1]+=moves[i][1]
-
w.coords(flake[i],p[0],p[1])
-
if(p[1]>310):
-
w.coords(flake[i],random.randrange(400),-10)
-
root.update_idletasks()
-
root.update()
-
except Tkinter.TclError:
-
pass
-
(there is a tiny fix)
-kudos Quote:
Originally Posted by bartonc I like it! It would have taken me a while to figure on using that error type. Thanks kudos. The critique is more for the benefit of others reading this.
But what happens if you get a tcl error? No window? No way to stop it?
These are mostly academic questions because (as you say) you don't us Tk much, but good for us to think about. I'm not actually going to break my tk installation to test it. |  | Moderator | | Join Date: Sep 2006 Location: Minden, Nevada, USA
Posts: 6,400
| | | re: Tkinter XMas Graphics
This is a pared down copy of this thread.
|  | Newbie | | Join Date: Dec 2006 Location: England!
Posts: 19
| | | re: Tkinter XMas Graphics
Heehee I love this! Very Christmassy.
Can't say I understand all the code, but I'm going to have a go at trying to understand it!
Hope you all had a lovely christmas(or other holiday you may have), and are enjoying the rest of the holidays.
|  | Moderator | | Join Date: Sep 2006 Location: Minden, Nevada, USA
Posts: 6,400
| | | re: Tkinter XMas Graphics Quote:
Originally Posted by Sushi Heehee I love this! Very Christmassy.
Can't say I understand all the code, but I'm going to have a go at trying to understand it!
Hope you all had a lovely christmas(or other holiday you may have), and are enjoying the rest of the holidays. Glad you like it. I dug it up just for you. Another one I didn't read as thoroughly as I should have. Kudos is really quite good.
In my book, it's Christmas with a capital C. Hope you had a good one too,
Barton
|  | Familiar Sight | | Join Date: Aug 2006
Posts: 195
| | | re: Tkinter XMas Graphics
Nice code, I never would have even considered to do this with python or anything else. Thanks for code.
|  | Expert | | Join Date: Jul 2006 Location: Norway
Posts: 112
| | | re: Tkinter XMas Graphics
Hi!
The code is actually quite simply, I will try to explain it... -
import Tkinter
-
import random
-
what does this part do? It simply states that we want to import the Tkinter module(that is what we use to open a window) and the random module (which allow us to generate random numbers). -
root = Tkinter.Tk()
-
w = Tkinter.Canvas(root, width=400, height=300, background="#000000")
-
In the first line, the window is opened. Next line, we add a canvas to the window. A canvas is basically the same thing as the one an artist use to paint on, something we will add things to, like text and snowflakes (*)
[code]
w.create_text(200,150,text="Happy Christmas 2006",font="Arial 20",fill="#ff0000")
w.create_text(200,170,text="from kudos",font="Arial 12",fill="#00ff00")
w.pack()
[code]
Here we basically place the "Happy Christmas 2006" and "from kudos" onto the canvas. Remeber, we created a Canvas object, and call the method "create_text" from this object.
Here we have two lists, one that stores each "*" (snowflake) and one that stores the "accelration" of each flake. Yes that is right, the flakes moves at different velocity to create the illusion of falling snow, more details below -
for i in range(50):
-
flake.append(w.create_text(random.randrange(400),random.randrange(300),text="*",fill="#ffffff",font="Times 30"))
-
moves.append([0.04 + random.random()/10,0.7 + random.random()])
-
Here we do the following. We decide that we will have 50 snowflakes. And we do create a "*" throught the create_text from the canvas object. We also decide that each flake should be placed randomly on the canvas from 0->400 horizontally and 0->300 vertically. In addition, we do store the value which the create_text method returns. We do this so we can access move each flake later (more about that below).
In the next line we decide on the velocity, i.e what speed the snowflake should fall to create the illusion of snowflakes. -
try:
-
while 1:
-
for i in range(len(flake)):
-
p = w.coords(flake[i])
-
p[0]+=moves[i][0]
-
p[1]+=moves[i][1]
-
w.coords(flake[i],p[0],p[1])
-
if(p[1]>310):
-
w.coords(flake[i],random.randrange(400),-10)
-
root.update_idletasks()
-
root.update()
-
except Tkinter.TclError:
-
pass
-
Okay, this is where the movements are done. We extract each coordinate of each snowflake (i.e. p = w.coords(flake[i])). This is obviously why we needed to store the value returned from create_text method above. We update position of the snowflake, and moves the snowflake accordingly. If the vertical position of the snowflake is below the canvas (i.e 310) we do the following: Place the snowflake at the top of the canvas, and place it randomly horizontal (so each snowflake seems to be different). And thats basically it! This loop where things are updated is the basis for most computer games and graphical demonstrations. A fun project that one could try out is to create a scrolling star background (found in countless space shooting games). scrolling starfield (java applet)
Hope this helps
-kudos Quote:
Originally Posted by Sushi Heehee I love this! Very Christmassy.
Can't say I understand all the code, but I'm going to have a go at trying to understand it!
Hope you all had a lovely christmas(or other holiday you may have), and are enjoying the rest of the holidays. |  | Moderator | | Join Date: Sep 2006 Location: Minden, Nevada, USA
Posts: 6,400
| | | re: Tkinter XMas Graphics
Thanks, kudos. Have a happy New Year!
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,467 network members.
|