[Tutor] Building input for a function call

David Hutto smokefloat at gmail.com
Fri Nov 12 21:33:34 CET 2010


>
> Singled out the two lines for clarity:
>> 12:           self.objectsvars = 'menuitemhere','menuitemhere','menuitemhere','menuitemhere'
>> 17:           self.optionmenu = OptionMenu(self.frame,self.var,self.objectsvars)
>
>
> Because in the first case, you're passing 3 items to OptionMene: self.frame, self.var & a tuple. The tuple itself contains four items, but it is still a single item as passed to OptionMenu.

I found out it was a tuple after I posted, which is why the error. It becomes
 OptionMenu(self.frame,self.var, ('menuitemhere' , 'menuitemhere' ,
'menuitemhere' , 'menuitemhere'))

This is how it's supposed to be init'd:
 OptionMenu(self.frame,self.var, 'menuitemhere' , 'menuitemhere' ,
'menuitemhere' , 'menuitemhere')
Where the args are a series of single strings, the first two are
self.frame(which is the parentframe
and self.var(which is a starting reference for the menu)

> In the second case, you're passing 6 items total.
>
It can be as many as I need, in the first email I was trying to state
that I have a list
of random length that has to be placed into those end values as args.

> If you want to use self.objectsvars, using the asterisk notation to expand your list or tuple:
> OptionMenu(self.frame, self.var, *self.objectsvars)
by expand, do you mean remove the tuple value, and leave the
contents(I'll try either way)
>
> Though I believe (haven't tried) that that actually doesn't work (depending on how OptionMenu.__init_ is defined), and you may need to pack everything into a single tuple, and expand that:
> OptionMenu( *((self.frame, self.var) + self.objects))
>
> So in that case, I'm adding a 2-tuple and a 4-tuple, and then expanding that. I need extra parentheses, otherwise only the 2-tuple gets expanded.

In think this would work in the apply() method I tried, which took a
tuple argument, but I didn't
need backward compatability, so I was using the function outright.
Again I'll see if that will work.


>
> Which of the above two cases you need, depends on how OptionMenu.__init__ is declared:
>
>  def __init__(self, frame, var, *args)
>
> should allow for the first, while
>
>  def __init__(self, *args)
>
> will work in the second case, since frame and var aren't picked up separately when calling the function.
>
>
> Google and read around for 'Python args kwargs', if this went somewhat over your head. Or just try and play with it some more.

Probably both, but I don't think that's the appropriate search term I
need at this point.


This is my current revision which writes a python file then calls the
function, but not quite right yet:

	def obOpMenu(self):
		print 'Dummy' #This is my tony robins help
		self.oblist = self.canvas.find_withtag('object')
		print self.oblist
		self.list = []
		for item in self.oblist:
			self.itemname = self.canvas.gettags(item)
			if self.itemname[1] != 'default':
				self.list.append(self.itemname[1])
		print self.list
		self.objectsvars = str(self.list).strip('[]')
		print self.objectsvars

		self.listlen = len(self.list)

		self.var = StringVar(self.root)
		self.var.set(self.objectsvars[0])
		self.f1 = open('/home/david/pythonfiles/pythonscripts/roughdraftapps/kubit_kaaba/other.py','w')
		self.f1.write("""from Tkinter import *
def thisDef(frame,var):
	optionmenu = OptionMenu(frame,var,%s)
	return optionmenu""" % (str(self.list).strip('[]')))
		self.f1.close()

		self.optionmenu = thisDef(self.frame,self.var)
		print self.optionmenu,'*'


More information about the Tutor mailing list