before I forget it...Tkinter XMas Graphics  | Expert | | Join Date: Jul 2006 Location: Norway
Posts: 118
| | -
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
| | | re: before I forget it...Tkinter XMas Graphics
Very cool! Thanks kudos. Happy Christmas to you, too.
Nice, elegant piece of software, my friend. Keep posting,
Barton
|  | Moderator | | Join Date: Sep 2006 Location: Minden, Nevada, USA
Posts: 6,403
| | | re: before I forget it...Tkinter XMas Graphics Quote:
Originally Posted by kudos -
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 I do, howerver, have one critique:
The naked except opens the door to bad things (especially when all you do is pass). If, for example, you should run out of memory (or something along those lines) your loop will keep running, which could be very bad indeed. The best practice is to narrow the focus of any try block to errors that you WANT to handle, then let built-in error handling take over. If you really want to catch all error that can possibly occur (naked except), then you should (at least) break out of the loop. Typically, this is called "over wrapping" the try block. Otherwise, I really like the graphics and the simplicity of you code.
|  | Expert | | Join Date: Jul 2006 Location: Norway
Posts: 118
| | | re: before I forget it...Tkinter XMas Graphics
Very true. I wrote this at the end of the day at work (+ I hardly ever use Tkinter)
This should be better: -
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 TclError:
-
pass
-
-kudos Quote:
Originally Posted by bartonc I do, howerver, have one critique:
The naked except opens the door to bad things (especially when all you do is pass). If, for example, you should run out of memory (or something along those lines) your loop will keep running, which could be very bad indeed. The best practice is to narrow the focus of any try block to errors that you WANT to handle, then let built-in error handling take over. If you really want to catch all error that can possibly occur (naked except), then you should (at least) break out of the loop. Typically, this is called "over wrapping" the try block. Otherwise, I really like the graphics and the simplicity of you code. |  | Moderator | | Join Date: Sep 2006 Location: Minden, Nevada, USA
Posts: 6,403
| | | re: before I forget it...Tkinter XMas Graphics Quote:
Originally Posted by kudos Very true. I wrote this at the end of the day at work (+ I hardly ever use Tkinter)
This should be better: -
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 TclError:
-
pass
-
-kudos 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.
|  | Expert | | Join Date: Jul 2006 Location: Norway
Posts: 118
| | | re: before I forget it...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,403
| | | re: before I forget it...Tkinter XMas Graphics
Dang! You got me! I missed that. I wish you had pointed that out when I started this rant. Now I have to go and take it all back. You are always one step ahead of me. Keep it up.
|  | | | | 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.
|