【问题标题】:i cant get the index value of selected item in listbox in tkinter我无法在 tkinter 的列表框中获取所选项目的索引值
【发布时间】:2019-10-25 09:51:43
【问题描述】:

我正在使用数据库处理 Tkinter 列表框,我无法获取列表框中所选项目的索引值

def admin_get():
    global selection
    win = Tk()
    win.geometry('500x400')
    listbox1 = Listbox(win,selectmode=SINGLE,width=60)
    listbox1.place(x=50,y=100)

    cursor.execute("SELECT * FROM admin")
    for i in cursor:
        listbox1.insert(END,i)
        #print(i)

    button = Button(win, text='change state', command=state)
    button.place(x=90, y=300)
    selection = listbox1.curselection(ACTIVE)
    win.mainloop()
def state():
    #messagebox.showinfo("alert",'Changed')
    print(selection)

【问题讨论】:

    标签: tkinter listbox python-3.6


    【解决方案1】:

    函数 listbox1.curselection() 没有参数。 根据它的文档

    [r]返回一个元组,其中包含所选元素的行号,从 0 开始计数。如果未选择任何内容,则返回一个空元组。

    补充: 如果元组为空,您可以检测到

    if selected:
        #some code
    

    只有当列表框的一个项目被选中时才会执行代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 2023-03-15
      • 2020-03-17
      • 2015-09-23
      • 2018-05-14
      • 1970-01-01
      • 2015-10-01
      相关资源
      最近更新 更多