[tkinter][原创]listbox-列表框的增删改

Tkinter listbox-列表框的操作

import tkinter as tk
import tkinter.colorchooser

'''listbox的增删改'''
window = tk.Tk()


def b1_click():
    item = ['apple', 'watermenon', 'orange']
    lb.insert(tk.END, item)


b1 = tk.Button(window, text="添加数据", command=b1_click)
b1.pack()


def b2_click():
    lb.insert(1, 'haha')


b2 = tk.Button(window, text="插入", command=b2_click)
b2.pack()


def b3_click():
    lb.delete(0)  # 删除第0的元素


b3 = tk.Button(window, text="删除", command=b3_click)
b3.pack()


def b4_click():
    lb.delete(0, tk.END)  # 清空列表


b4 = tk.Button(window, text="清空列表", command=b4_click)
b4.pack()


def b5_click():
    value = lb.get(lb.curselection())
    print(value)


b5 = tk.Button(window, text="获取选中数据", command=b5_click)
b5.pack()


def b6_click():
    print(lb.size())

b6 = tk.Button(window, text="总数", command=b6_click)
b6.pack()

var = tk.StringVar()
var.set((11, 22, 33, 44, 'aa'))
lb = tk.Listbox(window, listvariable=var)
lb.pack()

window.mainloop()

你可能感兴趣的:(Python)