Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
810 views
in Technique[技术] by (71.8m points)

python - changing order of items in tkinter listbox

Is there an easier way to change the order of items in a tkinter listbox than deleting the values for specific key, then re-entering new info?

For example, I want to be able to re-arrange items in a listbox. If I want to swap the position of two, this is what I've done. It works, but I just want to see if there's a quicker way to do this.

def moveup(self,selection):
    value1 = int(selection[0]) - 1 #value to be moved down one position
    value2 = selection #value to be moved up one position
    nameAbove = self.fileListSorted.get(value1) #name to be moved down
    nameBelow = self.fileListSorted.get(value2) #name to be moved up

    self.fileListSorted.delete(value1,value1)
    self.fileListSorted.insert(value1,nameBelow)
    self.fileListSorted.delete(value2,value2)
    self.fileListSorted.insert(value2,nameAbove)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is there an easier way to change the order of items in a tkinter listbox than deleting the values for specific key, then re-entering new info?

No. Deleting and re-inserting is the only way. If you just want to move a single item up by one you can do it with only one delete and insert, though.

def move_up(self, pos):
    """ Moves the item at position pos up by one """

    if pos == 0:
        return

    text = self.fileListSorted.get(pos)
    self.fileListSorted.delete(pos)
    self.fileListSorted.insert(pos-1, text)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...