Tkinter——③Listbox

Tkinter——Listbox

python入门常用操作:https://blog.csdn.net/qq_33302004/article/details/112859327

 

import tkinter as tk

window = tk.Tk()
window.title('Listbox')
window.geometry('400x600')

# label 
label_text = tk.StringVar()
label = tk.Label(window, width=15, height=2, background='green', textvariable=label_text, font = ('Helvetica', 12))
label.pack()

# button
def print_select():
    global label_text
    label_text.set(listbox.get(listbox.curselection()))
    return

def delete():
    listbox.delete(2)
    return

button = tk.Button(window, text='选 择', font = ('Helvetica', 12), command=print_select)
button2 = tk.Button(window, text='删除位置2处的内容', font = ('Helvetica', 12), command=delete)
button.pack()
button2.pack()

# Listbox
# 列表的变量
listbox_content = tk.StringVar()
listbox_content.set([11,22,33,44])
listbox = tk.Listbox(window,
                     listvariable=listbox_content, 
                     font = ('Helvetica', 12))
# 向listbox对象中插入新的值
list_item = [1,2,3,4]
for item in list_item:
    listbox.insert('end',item)
listbox.insert(1,'在index=1处插入')
listbox.insert(2,'在index=2处插入')
listbox.pack()


window.mainloop()

效果:

Tkinter——③Listbox_第1张图片

你可能感兴趣的:(python,tkinter,python,window,listbox,insert)