Python GUI编程(Tkinter)——Listbox

#pack有先后次序!!
import tkinter as tk
window = tk.Tk() #新建窗口对象
window.title('my window') #窗口名
window.geometry('200x200')#窗口大小

variable_1 = tk.StringVar()#新建文本变量
label = tk.Label(window,bg='yellow',width=4,textvariable=variable_1)
label.pack()

def print_selection():
    value = listbox.get(listbox.curselection())
    variable_1.set(value)

button = tk.Button(window,text='print selection',
                   width=15,height=2,command=print_selection)#新建按钮,命令关联到print_selection这个函数
button.pack()#放置按钮

variable_2 = tk.StringVar()#新建文本变量
variable_2.set((11,22,33,44,55))
listbox = tk.Listbox(window,listvariable=variable_2)
#对listbox进行添加删除等操作
list_items = ['!','@','$','%']
for item in list_items:
    listbox.insert('end',item)
listbox.insert(1,'first')
listbox.insert(2,'second')
listbox.delete(2)
listbox.pack()

window.mainloop()#持续刷新window



posted @ 2020-07-22 06:21  zjx_thu  阅读(248)  评论(0编辑  收藏  举报
/* 看板娘 */