Python 3.x Tkinter中的拖放列表 从tkinter导入* 将tkinter作为tk导入 root=tk.tk() def添加多首歌曲(): #循环通过以填充列表框 对于范围内的歌曲(11): 播放列表框。插入(结束,歌曲) playlist\u box=tk.Listbox(root,bg=“black”,fg=“green”,width=60,selectbackground=“green”,selectforeground=“black”,font=20) 播放列表框网格(行=0,列=0) 添加多首歌曲() 类DragDropListbox(tk.Listbox): “”“带有拖放式条目重新排序的Tkinter列表框。”“” def _初始功率(自功率、主功率,**kw): 千瓦['selectmode']=tk.单 tk.Listbox.\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu self.bind(“”,self.setCurrent) self.bind(“”,self.shiftSelection) self.curIndex=无 def setCurrent(自身、事件): self.curIndex=自最近(事件y) def换档选择(自身、事件): i=自最近(事件y) 如果iself.curIndex: x=自我获取(i) 删除(i) 自我插入(i-1,x) self.curIndex=i

Python 3.x Tkinter中的拖放列表 从tkinter导入* 将tkinter作为tk导入 root=tk.tk() def添加多首歌曲(): #循环通过以填充列表框 对于范围内的歌曲(11): 播放列表框。插入(结束,歌曲) playlist\u box=tk.Listbox(root,bg=“black”,fg=“green”,width=60,selectbackground=“green”,selectforeground=“black”,font=20) 播放列表框网格(行=0,列=0) 添加多首歌曲() 类DragDropListbox(tk.Listbox): “”“带有拖放式条目重新排序的Tkinter列表框。”“” def _初始功率(自功率、主功率,**kw): 千瓦['selectmode']=tk.单 tk.Listbox.\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu self.bind(“”,self.setCurrent) self.bind(“”,self.shiftSelection) self.curIndex=无 def setCurrent(自身、事件): self.curIndex=自最近(事件y) def换档选择(自身、事件): i=自最近(事件y) 如果iself.curIndex: x=自我获取(i) 删除(i) 自我插入(i-1,x) self.curIndex=i,python-3.x,tkinter,Python 3.x,Tkinter,##我发现这段代码在tkinter列表中具有拖放功能。我将它与示例代码一起使用。但是,我无法让它在所附的代码中工作。我还在学习Python。在创建播放列表框时,应该使用类DragDropListbox而不是tk.Listbox: 将tkinter作为tk导入 def添加多首歌曲(): #循环通过以填充列表框 对于范围内的歌曲(11): 播放列表框插入(tk.END,歌曲) 类DragDropListbox(tk.Listbox): “”“带有拖放式条目重新排序的Tkinter列表框。”“” de

##我发现这段代码在tkinter列表中具有拖放功能。我将它与示例代码一起使用。但是,我无法让它在所附的代码中工作。我还在学习Python。

在创建
播放列表框时,应该使用类
DragDropListbox
而不是
tk.Listbox

将tkinter作为tk导入
def添加多首歌曲():
#循环通过以填充列表框
对于范围内的歌曲(11):
播放列表框插入(tk.END,歌曲)
类DragDropListbox(tk.Listbox):
“”“带有拖放式条目重新排序的Tkinter列表框。”“”
def _初始功率(自功率、主功率,**kw):
千瓦['selectmode']=tk.单
tk.Listbox.\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
self.bind(“”,self.setCurrent)
self.bind(“”,self.shiftSelection)
self.curIndex=无
def setCurrent(自身、事件):
self.curIndex=自最近(事件y)
def换档选择(自身、事件):
i=自最近(事件y)
如果iself.curIndex:
x=自我获取(i)
删除(i)
自我插入(i-1,x)
self.curIndex=i
root=tk.tk()
playlist\u box=DragDropListbox(root,bg=“black”,fg=“green”,width=60,selectbackground=“green”,selectforeground=“black”,font=20)
播放列表框网格(行=0,列=0)
添加多首歌曲()
root.mainloop()

请注意,不建议像下面这样导入tkinter:

从tkinter导入*
将tkinter作为tk导入
只需使用
导入tkinter作为tk

from tkinter import *
import tkinter as tk
root = tk.Tk()

def add_many_songs():
    # Loop thru to fill list box
    for song in range(11):
        playlist_box.insert(END, song)
        
        
playlist_box =tk.Listbox(root,bg="black", fg="green", width=60, selectbackground="green", selectforeground='black',font = 20)
playlist_box.grid(row=0, column=0)

add_many_songs()

class DragDropListbox(tk.Listbox):
    """ A Tkinter listbox with drag'n'drop reordering of entries. """
    def __init__(self, master, **kw):
        kw['selectmode'] = tk.SINGLE
        tk.Listbox.__init__(self, master, kw)
        self.bind('<Button-1>', self.setCurrent)
        self.bind('<B1-Motion>', self.shiftSelection)
        self.curIndex = None

    def setCurrent(self, event):
        self.curIndex = self.nearest(event.y)

    def shiftSelection(self, event):
        i = self.nearest(event.y)
        if i < self.curIndex:
            x = self.get(i)
            self.delete(i)
            self.insert(i+1, x)
            self.curIndex = i
        elif i > self.curIndex:
            x = self.get(i)
            self.delete(i)
            self.insert(i-1, x)
            self.curIndex = i