双击操作后禁用列表框 - Python

时间:2018-03-15 17:49:16

标签: python tkinter

使列表框显示并能够借助几个示例示例解析列表框中的选定值,但在选择后无法禁用

def createlistBox:

    list1 = ['apple','mango']

    listBOXName = tk.Listbox(root,selectmode = "SINGLE")
    listBOXName.grid()

    for j in range(len(list1)):
        listBOXName.insert(tk.END,list1 [j])

    listBoxName.bind("<Double-Button-1>", selectlist1)

def selectlist1(evnt):

    lst = evnt.widget
    index = int(lst.curselection()[0])
    value = lst.get(index)

我想在调用selectlist1

时禁用该列表

1 个答案:

答案 0 :(得分:0)

您可以使用w.configure(state=tk.DISABLED)禁用列表框。这里w指的是小部件。

测试脚本:

# tkinter modules
import tkinter as tk


class App(tk.Frame):

    def __init__(self, parent=None, *args, **options):
        # Initialise App Frame
        tk.Frame.__init__(self, parent)
        self.parent = parent

        self.createlistBox()


    def createlistBox(self):
        list1 = ['apple','mango']

        listBOXName = tk.Listbox(self, selectmode = "SINGLE")
        listBOXName.grid()

        for j in range(len(list1)):
            listBOXName.insert(tk.END,list1 [j])

        listBOXName.bind("<Double-Button-1>", self.selectlist1)


    def selectlist1(self, evnt):

        lst = evnt.widget
        index = int(lst.curselection()[0])
        value = lst.get(index)
        lst.configure(state=tk.DISABLED) #Add this command after selection



if __name__ == '__main__':
    root = tk.Tk()
    root.geometry('400x350+300+300')

    app = App(root)
    app.grid(row=0, column=0, sticky='nsew')

    root.rowconfigure(0, weight=1)
    root.columnconfigure(0, weight=1)

    root.mainloop()