This is another problem in the Zelle book. -
def drawCircle(win, centre, radius, colour):
-
circle = Circle(radius)
-
circle.setFill(colour)
-
circle.setWidth(2)
-
circle.draw(win)
-
-
def eyes(row, columns):
-
win = GraphWin()
-
drawCircle(win, centre, 50, "white")
-
drawCircle(win, centre, 25, "blue")
-
drawCircle(win, centre, 10, "black")
How do I loop this code so that when I call the function eyes(2,4) it outputs 8 eyes on the graphic window for example
OOOO
OOOO
Please help me find a sloution.
26 9397
I might rather define a function that made 1 eye with a given centre. Then make the eyes call that function.
Be that as it may, you're going to need a nested loop -
for i in range(rows):
-
for j in range(columns):
-
#draw a eye at position i, j
-
I still don't understand it. Please do not mind, but can you show me the solution because I seriously can't figuare it out, I been trying for hours.
Have you managed to get it to print a circle yet? Have you tried with the functions you've got? Once you can do that, the rest should be easy.
- from graphics import*
-
def drawCircle(win, radius, colour):
-
circle = Circle(radius)
-
circle.setFill(colour)
-
circle.setWidth(2)
-
circle.draw(win)
-
-
def drawEye():
-
win = GraphWin()
-
drawCircle(win, 50, "white")
-
drawCircle(win, 25, "blue")
-
drawCircle(win, 10, "black")
-
-
def eightEyes(row,columns):
-
-
for i in range(rows):
-
for j in range(columns):
-
print i, j
this is my code at the moment, yes I can get one cricle to print. but it does print 8 cricles (like below)
0000
0000
As I missing something from the code, if I am can you correct the code, please.
Did you mean it does not print 8 circles?
What was the output of your code?
What did you run to get the circle to print?
- def drawCircle(win, centre, radius):
-
circle = Circle(centre, radius)
-
circle.setFill(colour)
-
circle.setWidth(2)
-
circle.draw(win)
-
-
def drawEyes():
-
win = GraphWin()
-
centre = Point(55,55)
-
drawCircle(win, centre, 50, "white")
-
drawCircle(win, centre, 25, "blue")
-
drawCircle(win, centre, 10, "black")
-
-
def eyes(rows,columns):
-
win = GraphWin("", 100 * rows, 100 * columns)
-
for i in range(rows):
-
for j in range(columns):
-
eyes(win, centre, 50)
At the moment i can't output 8 cricles in the graphic window and I don't known where I have gone wrong. Please help!
bvdet 2,851
Expert Mod 2GB
In your code, you are calling function eyes() recursively. I think you should be calling drawCircle() instead. In function eyes(), variable centre has not been defined.
You need some way to advance the calculation of the circle center point. I am not familiar with your graphics package, but in Tkinter, you create a circle by calculating the upper left and lower right coordinates on the canvas.
Let's assume you want a space between the circles. The width of your canvas would be:
The upper left and lower right coordinates for the circle in any given column, row would be - pt1 = Point(space/2.0, space/2.0)
-
pt2 = pt1+Point(radius*2, radius*2)
Then you iterate on columns and rows: - for i in range(0, columns):
-
for j in range(0, rows):
-
xy = pt1.x+i*(radius*2+space), \
-
pt1.y+j*(radius*2+space), \
-
pt2.x+i*(radius*2+space), \
-
pt2.y+j*(radius*2+space)
-
w.create_oval(xy, fill='red')
Point objects in my code have an addition overload(__add__).
Here's the full code to create circles in Tkinter: - from Tkinter import *
-
-
class Point(object):
-
def __init__(self, x=0.0, y=0.0):
-
self.x = float(x)
-
self.y = float(y)
-
def __add__(self, other):
-
return Point(self.x+other.x, self.y+other.y)
-
-
def drawCircles(columns, rows, radius=100, space=0):
-
root = Tk()
-
canvas_width = columns*(radius*2+space)
-
canvas_height = rows*(radius*2+space)
-
-
w = Canvas(root, width=canvas_width, height=canvas_height)
-
# create a black background
-
w.create_rectangle(0,0,canvas_width,canvas_height,fill="black")
-
# calculate UL and LR coordinates in column 1, row 1
-
pt1 = Point(space/2.0, space/2.0)
-
pt2 = pt1+Point(radius*2, radius*2)
-
for i in range(0, columns):
-
for j in range(0, rows):
-
# xy = UL.x, UL.y, LR.x, LR.y
-
xy = pt1.x+i*(radius*2+space), \
-
pt1.y+j*(radius*2+space), \
-
pt2.x+i*(radius*2+space), \
-
pt2.y+j*(radius*2+space)
-
# create a circle with red fill
-
w.create_oval(xy, fill='red')
-
-
w.pack()
-
root.mainloop()
-
-
if __name__ == '__main__':
-
drawCircles(4, 3, 125)
-
drawCircles(4, 2, 75, 20)
-
Post back with any questions you have.
@OzzyB
Hi. I think you're close. I'm also not familiar with the package you're using, but if drawEyes works, then you should be very close.
Firstly, change draw eyes so that it draws where you tell it to, rather than centred on (55,55): -
def drawEye(x,y):
-
win = GraphWin()
-
centre = Point(x,y)
-
drawCircle(win, centre, 50, "white")
-
drawCircle(win, centre, 25, "blue")
-
drawCircle(win, centre, 10, "black")
-
Now drawEye(55,55) should do the same as drawEyes() did before, but you've added flexibility! Note I've changed it to drawEye because it draws just one eyes, not many!
Next, adjust your eyes function to call drawEye in the loop: -
def eyes(rows,columns):
-
win = GraphWin("", 100 * rows, 100 * columns)
-
for i in range(rows):
-
for j in range(columns):
-
drawEye(i*100+50,j*100+50)
-
Note that the clever bit, is the we call drawEye 8 times (if rows=2 and columns=8 as you suggest), but each time with different parameters. if rows=2, the i will be 0 and 1. I know the size of my window is going to be 200 x 800. So I want it to be centred at 50 and 150. So i*100 is 0 and 100, and i*100+50 gives me the answer I want.
If this doesn't work, let us know why. Let us know what's happening with drawEye.
Good luck
I get a erorr and I am total clueless
NameError: global name 'win' is not defined
bvdet 2,851
Expert Mod 2GB @OzzyB
Show us the code that results in the error. It is probably something simple. This type of error happens when a variable is created in one function and you try to access it in another function. It's a matter of scope.
I corrected the error, but I have a new problem....:(
the cricle outputs on different windows, instead of one single window.
I use portable python 2.6
copy of my graphic module http://rapidshare.com/files/309307694/graphics.py.html
from graphics import *
import math - def drawCircle(win, centre, radius, colour):
-
circle = Circle(centre, radius)
-
circle.setFill(colour)
-
circle.setWidth(2)
-
circle.draw(win)
-
-
def drawEye(x,y):
-
win = GraphWin()
-
centre = Point(x,y)
-
drawCircle(win, centre, 50, "white")
-
drawCircle(win, centre, 25, "blue")
-
drawCircle(win, centre, 10, "black")
-
-
def windowsOfEyes(rows,columns):
-
win = GraphWin("", 100 * rows, 100 * columns)
-
for i in range(rows):
-
for j in range(columns):
-
drawEye(i*100+50,j*100+50)
Sorry if I am being annoying, this function is driving me nuts
bvdet 2,851
Expert Mod 2GB
You are creating multiple canvases. Create one canvas and draw each circle to that canvas. Try this: - def drawCircle(win, centre, radius, colour):
-
circle = Circle(centre, radius)
-
circle.setFill(colour)
-
circle.setWidth(2)
-
circle.draw(win)
-
-
def drawEye(win,x,y):
-
centre = Point(x,y)
-
drawCircle(win, centre, 50, "white")
-
drawCircle(win, centre, 25, "blue")
-
drawCircle(win, centre, 10, "black")
-
-
def windowsOfEyes(rows,columns):
-
win = GraphWin("", 100 * rows, 100 * columns)
-
for i in range(rows):
-
for j in range(columns):
-
drawEye(win,i*100+50,j*100+50)
I cannot download the graphics module without paying for it, so the code is untested. You may need to play with the size of your canvas.
bvdet 2,851
Expert Mod 2GB
I swapped columns and rows, imported the module and call each function or class module.call() so it is clear that the calls are made to graphics objects. Other than that, it works as I modified it in my previous post. - import graphics
-
-
def drawCircle(win, centre, radius, colour):
-
circle = graphics.Circle(centre, radius)
-
circle.setFill(colour)
-
circle.setWidth(2)
-
circle.draw(win)
-
-
def drawEye(win,x,y):
-
centre = graphics.Point(x,y)
-
drawCircle(win, centre, 50, "white")
-
drawCircle(win, centre, 25, "blue")
-
drawCircle(win, centre, 10, "black")
-
-
def windowsOfEyes(columns,rows):
-
win = graphics.GraphWin("", 100 * columns, 100 * rows)
-
for i in range(columns):
-
for j in range(rows):
-
drawEye(win,i*100+50,j*100+50)
-
-
windowsOfEyes(4,2)
-
I am so excited, it finally works. I couldn't have done it without your help thank.
So let's examine in more detail what's happening in the squares. You effectively have coordinates where you want the squares. They are:
(0,0) (0,1) (0,2) (0,3)
(1,0) (1,1) (1,2) (1,3), right.
So you get all these done by running two nested loops.
The first loop runs through all the columns, ie it runs from 0 to 3.
The for each column the second loop runs through all the rows from 0 to 1.
So let's spell this out even more explicitly, but for a 3x3 square:
Column 0 - Row 0,1,2
Column 1 - Row 0,1,2
Column 2 - Row 0,1,2.
Now for a triangle you'll want to do this:
Column 0 - Row 0,1,2
Column 1 - Row 1,2
Column 2 - Row 2
So in each case the row starts not from 0 (as in the square) but from the column number you're currently working with.
You might be able to figure it out from here...
Do I use the getX and getY?...or input the the coordinates in the range i & j.
I see no reference to getX or getY in the preceding discussion. Can you explain what you mean?
The statement - drawEye(win,i*100+50,j*100+50)
is an expression in that plots a point in row i and column j. Thus fiddling with the preceding loops will allow you to plot whatever combination of i and j you want.
I mean to be crude you could have: -
for i,j in [[0,0],[1,1],[2,2]]:
-
But there's a much more elegant solution to the triangle issue, which is also trying to teach you a specific lesson. Reread my previous post carefully, and you'll figure it out.
E.g. try this in terminal: -
>>> for i,j in [[1,1],[2,2],[3,3]]:
-
... print i,j
-
...
-
1 1
-
2 2
-
3 3
-
Play around in terminal, trying different things.
- def drawCircle(win, centre, radius, colour):
-
circle = Circle(centre, radius)
-
circle.setFill(colour)
-
circle.setWidth(2)
-
circle.draw(win)
-
-
def drawEye(win,x,y):
-
centre = Point(x,y)
-
drawCircle(win, centre, 50, "white")
-
drawCircle(win, centre, 25, "blue")
-
drawCircle(win, centre, 10, "black")
-
-
def windowsOfEyes(n):
-
win = GraphWin("", 100 * n, 100 * n)
-
for i in range((0,0),(0,1),(0,2),(0,3)):
-
for j in range((1,0),(1,1),(1,2),(1,3)):
-
drawEye(win,i*100+50,j*100+50)
when I call windowsOfEyes(3)
I get the following error
range expected at most 3 arguments, got 4
Can you corect the code, help me find a sloution.
Thanks
for i in range((0,0),(0,1),(0,2),(0,3)) has no real meaning.
Range can be used in a couple of ways. Try look it up in the docs, or google something like "python range", and see how range can be used.
By the way, what are you planning to use python for? You may need a different book, and perhaps some people can suggest resources for you. But it depends on your usage...
I figured it out at the end..I read your notes carefully...
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Talon |
last post by:
Hi all,
I am new to Tk, so please bear with me. I need someone better at math
than me to help me figure this out. I am drawing multiple arcs on the
same circle. All arcs start at 90 and have...
|
by: rdeaton |
last post by:
I need to design and code a Java program that calculates and prints the (D) diameter, the (C) circumference, or the (A) area of a circle, given the radius. The program inputs two data items: the...
|
by: Chua Wen Ching |
last post by:
Hi..
just wonder i draw a circle in the picturebox1
1) and i want to store the circle in memory (only circle)
when i store into bmp...
i want to see the circle with transparent...
|
by: Craig Parsons |
last post by:
Folks,
I have an image of a circle, which I am trying to straighten out into a
flat line. I am essentially wanting to look at the image, and read a
straight line from the centre, and then plot...
|
by: Pythor |
last post by:
I wrote the following code for a personal project. I need a function
that will plot a filled circle in a two dimensional array. I found
Bresenham's algorithm, and produced this code. Please tell...
|
by: Carl Gilbert |
last post by:
Hi
I am trying to plot a series of shapes in a circular fashion. All shapes
are evenly spaced with lines going between each shape. At present, all
shapes are the same size so I can easily plot...
|
by: saraaana |
last post by:
Given the center and a point on the circle, you can use this formula to find the radius of the circle. Write a program that prompts the user to enter the center and a point on the circle. The program...
|
by: heterodon7 |
last post by:
hello,
can anyone give me a clue or simple code on task:
for example we have in 2D an equation fo circle:
(x - 3)^2 + (y - 4)^2 = 25.
now the program must return for example a 40 pairs of...
|
by: DeadSilent |
last post by:
I have this code and for some reason I keep getting an error where it says "exporting non-public type through public api ".. I'm not sure why this keeps happening but it does so for getCircleInfo /...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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: 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...
|
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,...
|
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...
| |