944,147 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 541
  • Python RSS
Nov 5th, 2009
0

Drwaing Circles?

Expand Post »
hi, i want to be able to draw two eyes with are exactly the same in radius and colour etc next to eachother. So when i open the window the pair of eyes are there in the centre. I have managed to get one eye but i dnt have a clue to how to get one beside it, i am not sure hw to.. here is my code for the first eye and function should be calling the drawCircle six times.
please help
Python Syntax (Toggle Plain Text)
  1. from graphics import *
  2. import math
  3. def drawCircle(win, centre, radius, colour):
  4. circle = Circle(centre, radius)
  5. circle.setFill(colour)
  6. circle.setWidth(2)
  7. circle.draw(win)
  8.  
  9. def drawTarget():
  10. win = GraphWin()
  11. centre = Point(100,100)
  12. drawCircle(win, centre, 40, "white")
  13. drawCircle(win, centre, 20, "blue")
  14. drawCircle(win, centre, 10, "black")
Similar Threads
Reputation Points: 10
Solved Threads: 2
Light Poster
jaison2 is offline Offline
33 posts
since Nov 2009
Nov 5th, 2009
0
Re: Drwaing Circles?
One eye has to be positioned center_left, the other center_right ...
Python Syntax (Toggle Plain Text)
  1. from graphics import *
  2. import math
  3.  
  4. def drawCircle(win, centre, radius, colour):
  5. circle = Circle(centre, radius)
  6. circle.setFill(colour)
  7. circle.setWidth(2)
  8. circle.draw(win)
  9.  
  10. def drawTarget():
  11. win = GraphWin()
  12. centre_left = Point(50,100)
  13. centre_right = Point(150,100)
  14. drawCircle(win, centre_left, 40, "white")
  15. drawCircle(win, centre_left, 20, "blue")
  16. drawCircle(win, centre_left, 10, "black")
  17. drawCircle(win, centre_right, 40, "white")
  18. drawCircle(win, centre_right, 20, "blue")
  19. drawCircle(win, centre_right, 10, "black")
  20. # this keeps the window up/open
  21. win.getMouse()
  22.  
  23. drawTarget()
For those who don't know ...
Quote ...
John Zelle, Ph.D. teaches Python at Wartburg College
he is the author of graphics.py used by a number of schools instead
of Tkinter

Here is the graphics module (a thin wrapper for Tkinter) ...
http://mcsp.wartburg.edu/zelle/python/graphics.py
also has a nice documentation ...
http://mcsp.wartburg.edu/zelle/pytho...s/graphics.pdf

http://mcsp.wartburg.edu/zelle

Founded in 1852, Wartburg College is a nationally recognized selective
four-year liberal arts college of the Evangelical Lutheran Church in
America.

Wartburg College
100 Wartburg Blvd.
Waverly, IA 50677
(800) 772-2085

"Be Orange"
At this point in time module graphics.py works with Python2 only.
Install graphics.py in your working directory or the Lib/site-packages directory.
Last edited by vegaseat; Nov 5th, 2009 at 8:13 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 6th, 2009
0
Re: Drwaing Circles?
but hw would you get them in the exact centre of the graphics window.. is this fine?

Python Syntax (Toggle Plain Text)
  1. from graphics import *
  2. import math
  3.  
  4. def drawCircle(win, centre, radius, colour):
  5. circle = Circle(centre, radius)
  6. circle.setFill(colour)
  7. circle.setWidth(2)
  8. circle.draw(win)
  9.  
  10. def drawTarget():
  11. win = GraphWin()
  12. centre_left = Point(60,100)
  13. centre_right = Point(140,100)
  14. drawCircle(win, centre_left, 40, "white")
  15. drawCircle(win, centre_left, 20, "blue")
  16. drawCircle(win, centre_left, 10, "black")
  17. drawCircle(win, centre_right, 40, "white")
  18. drawCircle(win, centre_right, 20, "blue")
  19. drawCircle(win, centre_right, 10, "black")
  20. # this keeps the window up/open
  21. win.getMouse()
  22.  
  23. drawTarget()
Reputation Points: 10
Solved Threads: 2
Light Poster
jaison2 is offline Offline
33 posts
since Nov 2009
Nov 6th, 2009
0
Re: Drwaing Circles?
One way would be to say that the center should be at 1/2 width and height of the drawing window ...
python Syntax (Toggle Plain Text)
  1. from graphics import *
  2.  
  3. def drawCircle(win, centre, radius, colour):
  4. circle = Circle(centre, radius)
  5. circle.setFill(colour)
  6. circle.setWidth(2)
  7. circle.draw(win)
  8.  
  9. def drawTarget():
  10. w = 250
  11. h = 250
  12. win = GraphWin("Spooky Eyes", w, h)
  13. # center should be at 1/2 width and height
  14. center_w = w//2
  15. center_h = h//2
  16. centre_left = Point(center_w - 50, center_h)
  17. centre_right = Point(center_w + 50, center_h)
  18. drawCircle(win, centre_left, 40, "white")
  19. drawCircle(win, centre_left, 20, "blue")
  20. drawCircle(win, centre_left, 10, "black")
  21. drawCircle(win, centre_right, 40, "white")
  22. drawCircle(win, centre_right, 20, "blue")
  23. drawCircle(win, centre_right, 10, "black")
  24. # this keeps the window up/open
  25. win.getMouse()
  26.  
  27. drawTarget()
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 6th, 2009
0
Re: Drwaing Circles?
thankyou.. that makes sense nw.. had to tweak code a bit more for my requirements.. thx a lot..
Reputation Points: 10
Solved Threads: 2
Light Poster
jaison2 is offline Offline
33 posts
since Nov 2009
Nov 6th, 2009
0
Re: Drwaing Circles?
can i quickly just ask a question, would we be able to shrink the code anymore if it was just for one eye?.. as it has repetitive code?
Last edited by jaison2; Nov 6th, 2009 at 11:15 am.
Reputation Points: 10
Solved Threads: 2
Light Poster
jaison2 is offline Offline
33 posts
since Nov 2009
Nov 6th, 2009
0
Re: Drwaing Circles?
One eye would make it simpler ...
python Syntax (Toggle Plain Text)
  1. from graphics import *
  2.  
  3. def drawCircle(win, centre, radius, colour):
  4. circle = Circle(centre, radius)
  5. circle.setFill(colour)
  6. circle.setWidth(2)
  7. circle.draw(win)
  8.  
  9. def drawTarget():
  10. w = 250
  11. h = 250
  12. win = GraphWin("One Spooky Eye", w, h)
  13. # center should be at 1/2 width and height
  14. centre = Point(w//2, h//2)
  15. drawCircle(win, centre, 40, "white")
  16. drawCircle(win, centre, 20, "blue")
  17. drawCircle(win, centre, 10, "black")
  18. # click mouse to go on
  19. win.getMouse()
  20. win.close()
  21.  
  22. drawTarget()
You can streamline the whole thing a little more using a for loop ...
python Syntax (Toggle Plain Text)
  1. # draw one spooky eye in the center of the drawing window
  2. # calculate the center from width, height
  3.  
  4. from graphics import *
  5.  
  6. def drawCircle(win, centre, radius, colour):
  7. circle = Circle(centre, radius)
  8. circle.setFill(colour)
  9. circle.setWidth(2)
  10. circle.draw(win)
  11.  
  12. def drawTarget():
  13. w = h = 250
  14. win = GraphWin("One Spooky Eye", w, h)
  15. # center should be at 1/2 width and height
  16. centre = Point(w//2, h//2)
  17. for radius, color in ((40,"white"), (20,"blue"), (10,"black")):
  18. drawCircle(win, centre, radius, color)
  19. # click mouse to go on
  20. win.getMouse()
  21. win.close()
  22.  
  23. drawTarget()
Stop before the code gets too cryptic and hard to read and understand.
Last edited by vegaseat; Nov 6th, 2009 at 4:21 pm. Reason: optimize
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: List convert to string
Next Thread in Python Forum Timeline: 1 digit number 2 to 3 digit number 002





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC