remove item from scrolledlist

I have a directscrolledlist that shows a list of files in a folder. I want to be able to delete the file and update the list.

Can delete the file OK, but can’t figure out how to use removeitem() on DirectScrolledList.

the “item” parameter - how do I get that set?

It’s whatever you passed to addItem() earlier.


I added them with this code:

	filelist = os.listdir(GAME_STORY_CHAR_DATA)
	for charfile in filelist:
		f = charfile.replace(".hero","")	# drop file extension
		z = DirectButton(text = f, text_scale = 0.05, text_align =TextNode.ALeft,
			text_font=self.GAME_FONT, text_pos=(-.4,.7,0), relief = None,
			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),text2_bg=(0,0,0,1), command=self.screenCharEdit1, extraArgs=[f])

z is the object i pass to additem, but how would use that in removeitem.

Well, so you create a DirectButton on the fly and then add it to the DirectScrolledList. If you want to remove a particular DirectButton later, you’ll need to have a handle to the particular DirectButton you wish to remove.

You can get this handle by keeping it around in the first place (for instance, by saving z in a Python list as you pass it to the DirectScrolledList). But you can also get this list from dsl[‘items’], which is a list of the items you have passed to addItem().


OK, I have the list of items, but how do I refer to the specific item?

I tried dsl.getSelectedIndex(), but this always seems to return 0 for me.

Which specific item? The one that was clicked?

There’s nothing “selected” about the button that you clicked; it’s just one of many buttons, and you happened to click it. But you can save some identifying features along with the button, for instance you can pass an ID number in with the extraArgs to the command, or something like that.


ah, i see. thx!

I changed my list creation code by adding another Arg to the button command like this:

z = DirectButton(text = f, text_scale = 0.05, text_align =TextNode.ALeft,
					text_font=self.GAME_FONT, text_pos=(-.4,.7,0), relief = None,
					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),text2_bg=(0,0,0,1), command=self.screenCharEdit1, extraArgs=[f, self])

I added the 2nd arg to screenCharEdit1 like this:

def screenCharEdit1(self, charfile, lastButton):	# This screen pops up when character selected in list
		self.lastButton = lastButton

and finally, I used it in my delete button command like this:


no crashes, but the button doesn’t do anything. I printed what lastButton was and realized it’s the game object? (oops)

<__main__.MyGame instance at 0x03272238>

so what should the 2nd argument when i create the button be? I can’t use z

Right, so, use a unique number for each button, and then store that unique number in a table along with z. Then when you get the unique number back in the callback, look it up in the table and find z.

Or something like that. :slight_smile:


Thx for the help, I’ll try making a dictionary of id#s and button refs to get that to work.

On a related question. When I add a new file through my interface, I would like the new button added in alphabetical order. I’m assuming DirectScrollList doesn’t have an easy way to sort itself?

If not, I can imagine I’d figure out the position in the list I’d like to insert a new button in, but how do it tell .addItem to place the new button at a specific position?

OK, got the delete button working and updating the list. Thx again!

Anyone with a tip on how to add items in alpha order to the directscrollist?

just checking back - anyone ever tried to do this, i.e. add an item to a DSlist and keep teh list alphabetized?

I could try :

  • adding the item
  • sorting the key values of the item-dbutton dictionary (which I created for removing items)
    -clearing the dslist
  • putting back the items in sorted order

Would this work? It seems like a lot to do (and wouldn’t it be slow? I could theoretically have 100s of items in the list)

It does seem like a lot of work, and it does seem like it might be slow. You could certainly give it a try.

But you are stepping beyond the original design goals of the DirectScrolledList. In order to implement a sortable list, you might need to extend this class yourself.


so there’s no way to insert an item in a specified position in the list?

When you say extend, do you mean in Python?

I’m surprised no one has needed this before.

Well, there might be a way. You might be able to use Python commands to insert your elements into the middle of the list, or call list.sort(). It just wasn’t intended to be used in that way, so who knows whether this will work or not?

When I say extend, yes, I mean in Python. The DirectScrolledList is a Python class. You can write your own just like it, or you can subclass from it and redefine certain methods. Or you can edit it directly if you like.


Arrgghhh… looked through the directscrolllist class code and have tried a few things but no luck yet.

Interestingly, directscrollist[“items”].sort does not crash, but it also doesn’t seem to do anything I can see. Can’t seem to find a way to insert at a certain spot in the list, either.

Will try again tomorrow. Anyone with ideas, feel free to chime in :slight_smile:

Not that anyone noticed, but I’m back to report no progress by me on this so far… :slight_smile:

One question -
in one the DirectGUI tutorials, (#2 to be exact), there is this code:

self.chatList = DirectScrolledList(
        incButton_pos= (-0.4,0,-2.4),
        incButton_scale= (4,1,9),
        decButton_pos= (-0.4,0,-0.8),
        decButton_scale= (4,1,9),
        frameColor= (0,1,1,0),
        geom_scale = (15,15,15),
        geom_pos = (18,0,-2),
        numItemsVisible = 4)

Note sortOrder=2. What is this meant to do? I can’t find a ref to this in the manual or the api docs.

Sort order is the order in which gui and whatever is drawn.

Sorry, not following that.

What do you mean by order in which gui and whatever is drawn?