DirectScrolledList

OK, have a DirectScrolledList working which is showing me a list of files I have in a specific folder on my PC.

I would like to be able to select a file and then my code will read it and pull some info out of it so I can do something with it.

Going by the manual example, I created the list with DirectLabels (which I’m guessing aren’t clickable?). So I switched to DirectButtons which have the command param. My question is how would I write the command bit so it calls a function with the text of the button I clicked and then I use that value in my code. Or is there an easier way to watch for the event of someone clicking the list and returning the SelectedText? I did find this: getSelectedText(self)

but how do I “catch” the event?

hmm. just occurred to me that if I just create a new method (“chkList” or something) and call it from the button objects, I can just use the getSelectedText from the list to see which button you picked?

Going to try it now.

Well, my try to call a function from the command param in buttons worked, but how would I pass the button’s text as a param to that function?

And is there an easier way than switching to Directbuttons and passing params? Thx

Use the extraArgs of your button :

DirectButton(text=myFile, command=processFile, extraArgs=[fullPathToMyFile])

def processFile(aFile):
    print aFile

thx for the reply, I’ll give it a try when i get back to my dev pc.

Am I right in using directbuttons to do this? There’s no easier way with directlabels and some property/method of the DirectScrolledList itself?

DirectButtons are a perfectly legitimate use here.

I’m now using directbutton and added a command of ShowChrModel(hero) and for some reason it loads all the models in my list at once. (as if when i create the directbutton, it triggers the command.

		# get list of all files in characters folder and load into scroll list
		herolist = os.listdir(GAME_STORY + "/data/characters/")
		logFile("read all character files in " + GAME_STORY + "/data/characters/")
		for hero in herolist:
			z = DirectButton(text = hero.replace(".hero",""), text_scale = 0.05, text_align =TextNode.ALeft,
				text_font=self.GAME_FONT, text_pos=(-.4,.5,0), relief=DGG.FLAT,
				text0_fg=(0,0,1,1), text1_fg=(1,0,0,1), text2_fg=(1,1,0,1), text3_fg=(0,1,1,1),text0_bg=(0,0,0,1),
				command=self.showChrModel(hero))
			self.lstChar.addItem(z)
		
	def showChrModel(self, hero):
		print "in showmodel"
		f = open(GAME_STORY + "/data/characters/" + hero, "r")
		csvData = csv.reader(f, delimiter=';', quotechar='"')
		cData = csvData.next()
		f.close()		
		model = Actor.Actor(GAME_STORY + "/art/characters/" + cData[1] + "/" + cData[1], eval("ANIMS_"+cData[21]))
		model.setPos((.5, 0, -.65))
		model.setScale(.025)
		model.setH(200)
		model.reparentTo(self.scrChar)
		model.loop("idle")

also tried it with command=showChrModel, extraArgs=hero
and in that case, i get this error:
TypeError: showChrModel() takes exactly 2 arguments (18 given)

extraArgs=[hero]

tahnks, that did the trick.

Just so I understand…

  1. when i wrote
    command=self.showChrModel(hero))

was it really running the showChrModel function as I creted the directbutton? Is that how it behaves? Does this mean we never should pass a param in the command piece other than using extraArgs?

  1. Why does
    extraArgs([hero]) work
    and not extraArgs(hero)?
    is it just that extraArgs needs to have a list param?