472,353 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

before I forget it...Tkinter XMas Graphics

kudos
127 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. import Tkinter
  2. import random
  3.  
  4. root = Tkinter.Tk()
  5. w = Tkinter.Canvas(root, width=400, height=300, background="#000000")
  6. w.create_text(200,150,text="Happy Christmas 2006",font="Arial 20",fill="#ff0000")
  7. w.create_text(200,170,text="from kudos",font="Arial 12",fill="#00ff00")
  8. w.pack()
  9.  
  10. flake = [];
  11. moves = []
  12. for i in range(50):
  13.  flake.append(w.create_text(random.randrange(400),random.randrange(300),text="*",fill="#ffffff",font="Times 30"))
  14.  moves.append([0.04 + random.random()/10,0.7 + random.random()])
  15. try:
  16.  while 1:
  17.   for i in range(len(flake)):
  18.    p = w.coords(flake[i])
  19.    p[0]+=moves[i][0]
  20.    p[1]+=moves[i][1]
  21.    w.coords(flake[i],p[0],p[1])
  22.    if(p[1]>310):
  23.     w.coords(flake[i],random.randrange(400),-10)
  24.    root.update_idletasks() # redraw
  25.    root.update() # process events
  26. except:
  27.  pass
  28.  
run it on your computer..
-best wishes kudos
Dec 14 '06 #1
6 2443
bartonc
6,596 Expert 4TB
Very cool! Thanks kudos. Happy Christmas to you, too.
Nice, elegant piece of software, my friend. Keep posting,
Barton
Dec 15 '06 #2
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. import Tkinter
  2. import random
  3.  
  4. root = Tkinter.Tk()
  5. w = Tkinter.Canvas(root, width=400, height=300, background="#000000")
  6. w.create_text(200,150,text="Happy Christmas 2006",font="Arial 20",fill="#ff0000")
  7. w.create_text(200,170,text="from kudos",font="Arial 12",fill="#00ff00")
  8. w.pack()
  9.  
  10. flake = [];
  11. moves = []
  12. for i in range(50):
  13.  flake.append(w.create_text(random.randrange(400),random.randrange(300),text="*",fill="#ffffff",font="Times 30"))
  14.  moves.append([0.04 + random.random()/10,0.7 + random.random()])
  15. try:
  16.  while 1:
  17.   for i in range(len(flake)):
  18.    p = w.coords(flake[i])
  19.    p[0]+=moves[i][0]
  20.    p[1]+=moves[i][1]
  21.    w.coords(flake[i],p[0],p[1])
  22.    if(p[1]>310):
  23.     w.coords(flake[i],random.randrange(400),-10)
  24.    root.update_idletasks() # redraw
  25.    root.update() # process events
  26. except:
  27.  pass
  28.  
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.
Dec 15 '06 #3
kudos
127 Expert 100+
Very true. I wrote this at the end of the day at work (+ I hardly ever use Tkinter)

This should be better:

Expand|Select|Wrap|Line Numbers
  1. import Tkinter
  2. import random
  3.  
  4. root = Tkinter.Tk()
  5. w = Tkinter.Canvas(root, width=400, height=300, background="#000000")
  6. w.create_text(200,150,text="Happy Christmas 2006",font="Arial 20",fill="#ff0000")
  7. w.create_text(200,170,text="from kudos",font="Arial 12",fill="#00ff00")
  8. w.pack()
  9.  
  10. flake = [];
  11. moves = []
  12. for i in range(50):
  13.  flake.append(w.create_text(random.randrange(400),random.randrange(300),text="*",fill="#ffffff",font="Times 30"))
  14.  moves.append([0.04 + random.random()/10,0.7 + random.random()])
  15. try:
  16.  while 1:
  17.   for i in range(len(flake)):
  18.    p = w.coords(flake[i])
  19.    p[0]+=moves[i][0]
  20.    p[1]+=moves[i][1]
  21.    w.coords(flake[i],p[0],p[1])
  22.    if(p[1]>310):
  23.     w.coords(flake[i],random.randrange(400),-10)
  24.    root.update_idletasks()
  25.    root.update()
  26. except TclError:
  27.  pass
  28.  
-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.
Dec 15 '06 #4
bartonc
6,596 Expert 4TB
Very true. I wrote this at the end of the day at work (+ I hardly ever use Tkinter)

This should be better:

Expand|Select|Wrap|Line Numbers
  1. import Tkinter
  2. import random
  3.  
  4. root = Tkinter.Tk()
  5. w = Tkinter.Canvas(root, width=400, height=300, background="#000000")
  6. w.create_text(200,150,text="Happy Christmas 2006",font="Arial 20",fill="#ff0000")
  7. w.create_text(200,170,text="from kudos",font="Arial 12",fill="#00ff00")
  8. w.pack()
  9.  
  10. flake = [];
  11. moves = []
  12. for i in range(50):
  13.  flake.append(w.create_text(random.randrange(400),random.randrange(300),text="*",fill="#ffffff",font="Times 30"))
  14.  moves.append([0.04 + random.random()/10,0.7 + random.random()])
  15. try:
  16.  while 1:
  17.   for i in range(len(flake)):
  18.    p = w.coords(flake[i])
  19.    p[0]+=moves[i][0]
  20.    p[1]+=moves[i][1]
  21.    w.coords(flake[i],p[0],p[1])
  22.    if(p[1]>310):
  23.     w.coords(flake[i],random.randrange(400),-10)
  24.    root.update_idletasks()
  25.    root.update()
  26. except TclError:
  27.  pass
  28.  
-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.
Dec 15 '06 #5
kudos
127 Expert 100+
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)

Expand|Select|Wrap|Line Numbers
  1. import Tkinter
  2. import random
  3.  
  4. root = Tkinter.Tk()
  5. w = Tkinter.Canvas(root, width=400, height=300, background="#000000")
  6. w.create_text(200,150,text="Happy Christmas 2006",font="Arial 20",fill="#ff0000")
  7. w.create_text(200,170,text="from kudos",font="Arial 12",fill="#00ff00")
  8. w.pack()
  9.  
  10. flake = [];
  11. moves = []
  12. for i in range(50):
  13.  flake.append(w.create_text(random.randrange(400),random.randrange(300),text="*",fill="#ffffff",font="Times 30"))
  14.  moves.append([0.04 + random.random()/10,0.7 + random.random()])
  15. try:
  16.  while 1:
  17.   for i in range(len(flake)):
  18.    p = w.coords(flake[i])
  19.    p[0]+=moves[i][0]
  20.    p[1]+=moves[i][1]
  21.    w.coords(flake[i],p[0],p[1])
  22.    if(p[1]>310):
  23.     w.coords(flake[i],random.randrange(400),-10)
  24.    root.update_idletasks()
  25.    root.update()
  26. except Tkinter.TclError:
  27.  pass
  28.  
(there is a tiny fix)

-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.
Dec 15 '06 #6
bartonc
6,596 Expert 4TB
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.
Dec 15 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: rhmd | last post by:
I need to create image files (eg bmp or jpeg) of xy scatter graphs (i.e., graphs in which markers denote individual points; the markers need to be...
7
by: John Slimick | last post by:
I want to do a little Tkinter in my 1 credit python practicum, but I am having problems getting everything installed correctly. A sample of the...
7
by: SeeBelow | last post by:
Do many people think that wxPython should replace Tkinter? Is this likely to happen? I ask because I have just started learning Tkinter, and I...
5
by: Andrew | last post by:
Hi I was wondering if there is anyway in Tkinter to create GUIs using Graphics, like windows media player or other tools like that basically so...
8
by: Erik Johnson | last post by:
I am looking for some input on GUI libraries. I want to build a Python-driven GUI, but don't really understand the playing field very well. I have...
5
by: Ben Kovitz | last post by:
Hi, I just tried to run Tkinter on OS X 10.3.9 under Python 2.4.3, and I'm getting a bus error as soon as I call Tk(). Googling has turned up info...
2
by: David Lees | last post by:
Does anyone have advice on installing Tkinter on s Silicon Graphics machine (under IRIX 6, I think). The SysAdmin at work build Python 2.4.3 for me...
8
kudos
by: kudos | last post by:
import Tkinter import random root = Tkinter.Tk() w = Tkinter.Canvas(root, width=400, height=300, background="#000000")...
0
kudos
by: kudos | last post by:
Hi, a couple of months ago, I made a xmas chistmas "card" to this community(http://www.thescripts.com/forum/thread580722.html) Now its soon easter,...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.