changing order of items in tkinter listbox(更改 tkinter 列表框中的项目顺序)
问题描述
有没有比删除特定键的值然后重新输入新信息更简单的方法来更改 tkinter 列表框中的项目顺序?
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)
推荐答案
有没有比删除特定键的值然后重新输入新信息更简单的方法来更改 tkinter 列表框中的项目顺序?
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)
这篇关于更改 tkinter 列表框中的项目顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:更改 tkinter 列表框中的项目顺序


基础教程推荐
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01