# !/usr/bin/python3
from tkinter import *
import tkinter.messagebox
top = Tk()
top.geometry("400x450")
lbl = Label(top, text="tk列表框内值上下移动位置")
listbox = Listbox(top)
# 向上移动
def list_Up(filter_list):
a = listbox.get(ANCHOR) # 获取选择的列表值
p = filter_list.index(a) - 1 # 获取选择值在列表中的位置
if p == -1:
listbox.delete(0, END) # 清空列表框
elif p != -1: # 如果位置不等于-1
filter_list.insert(p, a) # 列表中插入位置p,值为选择的值
del filter_list[p + 2] # 删除掉原位置的值
listbox.delete(0, END) # 清空列表框
for item in filter_list: # 循环列表
listbox.insert(END, item) # 列表框最后插入值
tkinter.messagebox.showinfo("","向上移动了一个位置")
return filter_list
# 向下移动
def list_Down(filter_list):
a = listbox.get(ANCHOR)
p = filter_list.index(a) + 2
filter_list.insert(p, a)
del filter_list[p - 2]
listbox.delete(0, END)
for item in filter_list:
listbox.insert(END, item)
return filter_list
m = [ "0","1", "2", "3"]
for x in m:
listbox.insert(END, x)
btn = Button(top, text="向上移动", command=lambda: list_Up(m))
btn2 = Button(top, text="向下移动", command=lambda: list_Down(m))
lbl.pack()
listbox.pack()
btn2.pack()
btn.pack()
top.mainloop()