Tkinter XMas Graphics  | Expert | | Join Date: Jul 2006 Location: Norway
Posts: 118
# 1
Dec 14 '06
| | -
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,403
# 2
Dec 15 '06
| | | 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: 118
# 3
Dec 15 '06
| | | 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. |  | | | | Forums
Visit our community forums for general discussions and latest on Bytes
/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 229,155 network members.
|