Python 从Tkinter中的列表框中取消选择

Python 从Tkinter中的列表框中取消选择,python,tkinter,listbox,Python,Tkinter,Listbox,我只是想知道如何从thinter的列表框中取消选择。每当我点击列表框中的某个内容时,它会高亮显示,并且会加下划线,但当我点击屏幕的一侧时,列表框选择会保持高亮显示。即使单击按钮,所选内容仍保持下划线。例如:在下面的示例代码中,单击其中一个后,我无法从列表框选择中单击 from tkinter import * def Add(): listbox.insert(END, textVar.get()) root = Tk() textVar = StringVar() entry =

我只是想知道如何从thinter的列表框中取消选择。每当我点击列表框中的某个内容时,它会高亮显示,并且会加下划线,但当我点击屏幕的一侧时,列表框选择会保持高亮显示。即使单击按钮,所选内容仍保持下划线。例如:在下面的示例代码中,单击其中一个后,我无法从列表框选择中单击

from tkinter import *

def Add():
   listbox.insert(END, textVar.get())


root = Tk()

textVar = StringVar()
entry = Entry(root, textvariable = textVar)
add = Button(root, text="add", command = Add)
frame = Frame(root, height=100, width=100, bg="blue")
listbox = Listbox(root, height=5)
add.grid(row=0, column=0, sticky=W)
entry.grid(row=0, column=1, sticky=E)
listbox.grid(row=1, column=0)
frame.grid(row=1, column=1)

root.mainloop()

是的,这是列表框的正常行为。如果要更改,可以在列表框每次离开焦点时调用clear函数:

listbox.bind('<FocusOut>', lambda e: listbox.selection_clear(0, END))
listbox.bind(“”,lambda e:listbox.selection_clear(0,END))

使用Listbox小部件上的selectmode参数。 您可以再次单击所选项目,它将清除所选内容

请参见effbot链接:


我已经设法在Listbox小部件中创建了所需的功能,这样当用户单击Listbox中的相同项目或屏幕上的其他位置时,当前选择的项目将被取消选择。解决办法很简单

首先,我创建了一个绑定,这样当在窗口的任何位置按下鼠标左键时,就会执行一个取消选择列表框的功能

root.bind('<ButtonPress-1>', deselect_item)
然后我定义了取消选择列表框的函数,如下所示。首先,选择新项目(用户刚刚单击的项目)并与以前选择的项目进行比较。如果这是真的,则用户已单击列表框中已高亮显示的项目,因此列表框的选择被清除,从而删除所选项目。最后,该函数将以前选定的框更新为当前选定的框

def deselect_item(event):
    if listbox.curselection() == previous_selected:
            listbox.selection_clear(0, tkinter.END)
    previous_selected = listbox.curselection()
这方面的完整工作示例(在python 3.8.0中)如下所示:

import tkinter

class App(tkinter.Tk):
    def __init__(self):
        tkinter.Tk.__init__(self)
        self.previous_selected = None

        self.listbox = tkinter.Listbox(self)
        self.bind('<ButtonPress-1>', self.deselect_item)

        self.listbox.insert(tkinter.END, 'Apple')
        self.listbox.insert(tkinter.END, 'Orange')
        self.listbox.insert(tkinter.END, 'Pear')

        self.listbox.pack()

    def deselect_item(self, event):
        if self.listbox.curselection() == self.previous_selected:
                self.listbox.selection_clear(0, tkinter.END)
        self.previous_selected = self.listbox.curselection()

app = App()
app.mainloop()
导入tkinter
类应用程序(tkinter.Tk):
定义初始化(自):
tkinter.Tk.\uuuuu初始化\uuuuuuuuu(自)
self.previous_selected=无
self.listbox=tkinter.listbox(self)
self.bind(“”,self.disselect_项)
self.listbox.insert(tkinter.END,'Apple')
self.listbox.insert(tkinter.END,“橙色”)
self.listbox.insert(tkinter.END,“Pear”)
self.listbox.pack()
def取消选择项目(自身、事件):
如果选择了self.listbox.curselection()==self.previous\u:
self.listbox.selection\u clear(0,tkinter.END)
self.previous_selected=self.listbox.curseelection()
app=app()
app.mainloop()
def deselect_item(event):
    if listbox.curselection() == previous_selected:
            listbox.selection_clear(0, tkinter.END)
    previous_selected = listbox.curselection()
import tkinter

class App(tkinter.Tk):
    def __init__(self):
        tkinter.Tk.__init__(self)
        self.previous_selected = None

        self.listbox = tkinter.Listbox(self)
        self.bind('<ButtonPress-1>', self.deselect_item)

        self.listbox.insert(tkinter.END, 'Apple')
        self.listbox.insert(tkinter.END, 'Orange')
        self.listbox.insert(tkinter.END, 'Pear')

        self.listbox.pack()

    def deselect_item(self, event):
        if self.listbox.curselection() == self.previous_selected:
                self.listbox.selection_clear(0, tkinter.END)
        self.previous_selected = self.listbox.curselection()

app = App()
app.mainloop()