tkinter学习笔记_02

4、 多行输入框 text

# 按钮    # command 执行动作
def insert_point():
    var = e.get()
    t.insert('insert', var)

b = tk.Button(root, text='insert point', width=15, height=2, command=insert_point)
b.pack()

def insert_end():
    var = e.get()
    t.insert('end', var)   # t.insert(1.1, var)   第一行第一位插入

b2 = tk.Button(root, text='insert end', command=insert_end)
b2.pack()
# 多行输入框
t = tk.Text(root, height=2)
t.pack()

5、列表 listbox

var1 = tk.StringVar()   # 第一个字符串变量值
l = tk.Label(root, bg='yellow', width=4, textvariable=var1)  # textvariable 文本变量值
l.pack()

# 按钮    # command 执行动作
def print_selection():
    value = lb.get(lb.curselection())   # 光标选中的东西,拿出来
    var1.set(value)  # 显示到 提示框

b = tk.Button(root, text='print selection', width=15, height=2,
              command=print_selection)
b.pack()
# 列表
var2 = tk.StringVar()   # 第二个字符串变量值
var2.set((11,22,33,44))
lb = tk.Listbox(root, listvariable=var2)  # listvariable 列表变量值
list_items = [1,2,3,4]
for item in list_items:   # 循环插入
    lb.insert('end', item)
lb.insert(1, 'first')
lb.insert(2, 'second')
lb.delete(2)
lb.pack()

root.mainloop()

原文地址:https://www.cnblogs.com/lixy-88428977/p/9367037.html