how can i create 50 apples at different positions on screen without having to each time call the constructor 50 times?

import Tkinter as tk
from Tkinter import *
import time

class Apple(object):
  def __init__(self, a,b,c,d):
    self.rect = canvas.create_oval(a,b,c,d)


root=tk.Tk()
canvas = tk.Canvas(root, width=300, height=300, bg="#FFFFFF")
canvas.pack()

for x in range (0,50):
    apple="apple"+ str(x)
    #apple=Fruit(60,60,80,80)

root.mainloop()

how can i create 50 apples at different positions on screen without having to each time call the constructor 50 times?

Just put the call to the Apple constructor within your for loop and randomize the values a bit... or use a linear approach to the values... or whatever suits your purposes.

If you need to retain the object handles for each Apple object you can use a list and just use something like apple_list.append(Apple(x,y,z,w)) . Or you could use a combination of eval() and string formatting to generate variable names like my_apple1, my_apple2, etc.

Do you understand what I'm suggesting?

i've created a list and the apples are there just like i want them. but i can't name each apple. how can i use the eval to name each apple so that methods can be applied to them?

and can i shuffle my list? is there any in-built function?

Oh whoops, sorry I meant exec... I always mix the two up. Use it like the following:

>>> apple1
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'apple1' is not defined
>>> for x in xrange(1,6):
...     exec 'apple%s = %s' % (x, x * 2.5)
...     
>>> apple1
2.5
>>> apple2
5.0
>>> apple3
7.5
>>> apple4
10.0
>>> apple5
12.5
>>>

If you go the list route, there's a random.shuffle function in the random module. Additionally you can use each list element as the "name" for the variables like so:

>>> my_objects = []
>>> class test_class(object):
...     def __init__(self, param1):
...         self.p1 = param1
...     def test(self):
...         print 'Hi, my p1 is %s' % self.p1
...     
>>> for x in xrange(1,6):
...     my_objects.append(test_class('Test%s' % x))
...     
>>> my_objects
[<__main__.test_class object at 0x01D49BB0>, <__main__.test_class object at 0x01D49E30>, <__main__.test_class object at 0x01D49E70>, <__main__.test_class object at 0x01D49EB0>, <__main__.test_class object at 0x01D49A30>]
>>> for each_object in my_objects:
...     each_object.test()
...     
Hi, my p1 is Test1
Hi, my p1 is Test2
Hi, my p1 is Test3
Hi, my p1 is Test4
Hi, my p1 is Test5
>>> my_objects[2].test()
Hi, my p1 is Test3
>>>

Lets say you have a list of apples:
apples = [Apple(x, b, c, d) for x in range(0, 50)]

Why would you want to have a variable for each of the apples? There's no difference between:

apple1 = apples[1]     #done somewhere in the program
apple1.someFunction()

and

apples[1].someFunction

At some point you have to know which object you want to manipulate. If the exact object doesn't matter - lists are a even better choice.

-Vidaj-

thanks loads lads.. excellent work.. between how can i enter an integer value using a button and alert an error if not an integer?

ive managed to do it in the python shell but i need it using buttons, GUI. any example plz?

You cant just get a value from a button, buttons are basically used to call events and such, so you would need some kind of text entry and then when the button event was called use the assert keyword.

Assert is a keyword that raises an error if its requirements are not met.
http://www.rexx.com/~dkuhlman/python_101/python_101.html#SECTION004330000000000000000

Here is an example

#python 2.x code
i = int(raw_input("Enter the number 10"))

assert i == 10, "Error i does not equal 10"

HTH

This article has been dead for over six months. Start a new discussion instead.