[Tutor] Building input for a function call
David Hutto
smokefloat at gmail.com
Fri Nov 12 14:08:16 CET 2010
Hey Buddy Pals,
In the below *function*(if you can call it that yet:)
I'm trying to do the following:
interpolate this string:
'OptionMenu(self.frame,self.var,'%s','%s')'
with this list turned into variables:
['default1', 'default2']
which would become:
self.default1 = 'default1'
self.default2 = 'default2'
So that I have:
OptionMenu(self.frame,self.var,self.default1,self.default2)
or even:
OptionMenu(self.frame,self.var,'default1','default2')
Starting from the top:
from self.obtup I obtain a tuple:
(7, 8)
I then use the tuple to identify an object the
numbers in self.obtup represent, and build a list
from this in self.list:
['default1', 'default2']
I then build a string for a later to be used menu with:
self.string = 'OptionMenu(self.frame,self.var' + str(self.listlen * ',%s') + ')'
which gives:
self.string = OptionMenu(self.frame,self.var,%s,%s)
And then build a variable list self.list2 = ['self.default1 = ',
'self.default2 = ']
So that I have:
self.default1 =
self.default2 =
Then append to those the variables they represent so that I have
self.default1 = 'default1'
self.default2 = 'default2'
When I get to the step above, trying to use eval()(the data is restricted to
variable names, but I know a misnamed with a invalid character could cause
problems) in a loop, so that it uses
the combined as a variable, but get an error for the '='.
I've tried several different methods this seemed like the best course,
but maybe not.
*Note files self.f1 and self.f2 are temporary holders for:
OptionMenu(self.frame,self.var,'%s','%s') = where the %s's should be
replaced with the vars or strings.
and self.f2 is
'self.default1 = ', 'self.default2 = '
Which could probably be handled a better way than a file.
@trace
def obOpMenu(self):
import time
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.f1 = open('/home/david/pythonfiles/pythonscripts/roughdraftapps/kubit_kaaba/obmenu.txt','w')
self.f2 = open('/home/david/pythonfiles/pythonscripts/roughdraftapps/kubit_kaaba/obmenuvar.txt','w')
self.listlen = len(self.list)
self.string = 'OptionMenu(self.frame,self.var' + str(self.listlen *
",'%s'") + ')'
self.list2 = []
for line in self.list:
self.list2.append('self.%s = ' % (line))
print self.string, self.list2
self.count = 0
for num in range(len(self.list)):
print self.list2[num]
self.f2.write(str(self.list2).strip('[]'))
self.f2.close()
#time.sleep(1)
#self.f2 = open('/home/david/pythonfiles/pythonscripts/roughdraftapps/kubit_kaaba/obmenuvar.txt','r')
#self.f1.write(self.string % self.list2)
self.f1.close()
#time.sleep(1)
#self.f1 = open('/home/david/pythonfiles/pythonscripts/roughdraftapps/kubit_kaaba/obmenu.txt','r')
'''
#if len(str(self.oblist)) > 2:
for field in self.f1.read():
self.abc = q.strip().split( "," )
#self.var = StringVar(root)
#self.var.set(self.list[0])
#self.optionmenu = self.f1.read()
#self.optionmenu.grid(row = 1,column = 2)
'''
Thanks,
David
More information about the Tutor
mailing list