473,507 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter XMas Graphics

kudos
127 Recognized Expert New Member
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
8 2849
bartonc
6,596 Recognized Expert Expert
Very cool! Thanks kudos. Happy Christmas to you, too.
Nice, elegant piece of software, my friend. Keep posting,
Barton
Dec 15 '06 #2
kudos
127 Recognized Expert New Member
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 #3
bartonc
6,596 Recognized Expert Expert
This is a pared down copy of this thread.
Dec 26 '06 #4
Sushi
19 New Member
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.
Dec 26 '06 #5
bartonc
6,596 Recognized Expert Expert
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
Dec 26 '06 #6
Loismustdie129
195 New Member
Nice code, I never would have even considered to do this with python or anything else. Thanks for code.
Dec 27 '06 #7
kudos
127 Recognized Expert New Member
Hi!

The code is actually quite simply, I will try to explain it...

Expand|Select|Wrap|Line Numbers
  1. import Tkinter
  2. import random
  3.  
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).

Expand|Select|Wrap|Line Numbers
  1. root = Tkinter.Tk()
  2. w = Tkinter.Canvas(root, width=400, height=300, background="#000000")
  3.  
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.

Expand|Select|Wrap|Line Numbers
  1. flake = [];
  2. moves = []
  3.  
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

Expand|Select|Wrap|Line Numbers
  1. for i in range(50):
  2.  flake.append(w.create_text(random.randrange(400),random.randrange(300),text="*",fill="#ffffff",font="Times 30"))
  3.  moves.append([0.04 + random.random()/10,0.7 + random.random()])
  4.  
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.

Expand|Select|Wrap|Line Numbers
  1. try:
  2.  while 1:
  3.   for i in range(len(flake)):
  4.    p = w.coords(flake[i])
  5.    p[0]+=moves[i][0]
  6.    p[1]+=moves[i][1]
  7.    w.coords(flake[i],p[0],p[1])
  8.    if(p[1]>310):
  9.     w.coords(flake[i],random.randrange(400),-10)
  10.    root.update_idletasks()
  11.    root.update()
  12. except Tkinter.TclError:
  13.  pass
  14.  
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



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.
Dec 28 '06 #8
bartonc
6,596 Recognized Expert Expert
Thanks, kudos. Have a happy New Year!
Dec 28 '06 #9

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

Similar topics

9
7050
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,...
7
3139
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...
7
11882
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...
5
3681
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 ...
5
3541
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...
2
1656
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. ...
6
2526
kudos
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...
2
9008
kudos
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...
0
5913
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, and I hacked together something for easter as...
0
7110
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...
0
7314
Oralloy
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,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7482
tracyyun
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...
0
5623
agi2029
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,...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.