-
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
6 2521
Very cool! Thanks kudos. Happy Christmas to you, too.
Nice, elegant piece of software, my friend. Keep posting,
Barton
-
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.
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 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.
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.
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
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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 small polygons of various sizes, shapes, colors,...
|
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 problem is below:
------------------- The...
|
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 wonder if I
should abandon it in favor of...
|
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 the interface wouldn't be your standard interface
...
|
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 generally heard good things about wxPython. I...
|
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 other Tkinter bus errors, but not this one that...
|
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 on the SGI box, but it does not have Tkinter. ...
|
by: kudos |
last post by:
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...
|
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, and I hacked together something for easter as...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |