Tkinter教程之Listbox 列表部件

51changxuePython评论805 views阅读模式

Tkinter教程之Listbox 列表部件

# Listbox为列表框控件,它可以包含一个或多个文本项(text item),可以设置为单选或多选

'''''1.创建一个Listbox,向其中添加三个item'''
from tkinter import *
import tkinter as tk#引入tkinter模块

root = Tk()
lb = Listbox(root, height=5,)
for item in ['python', 'tkinter', 'widget']:
    lb.insert(END, item)
lb.pack()

#也可以赋值变量
var2 = tk.StringVar()
var2.set((11,22,33,44)) #为变量设置值
#创建Listbox
lb = Listbox(root, height=5,listvariable=var2)  #将var2的值赋给Listbox
print(var2.get())#获取条目
lb.pack()
'''''2.创建一个可以多选的Listbox,使用属性selectmaod'''
lb = Listbox(root, height=5,selectmode = MULTIPLE)
for item in ['python2','tkinter2','widget2']:
    lb.insert(END,item)
lb.pack()

'''''3这个属性selectmode还可以设置为BROWSE,可以通过鼠标来移动Listbox中的选中位置(不是移动item), 
这个属性也是Listbox在默认设置的值,这个程序与1.程序运行的结果的一样的。'''
lb = Listbox(root, height=5,selectmode = BROWSE)
for item in ['python','tkinter','widget']:
    lb.insert(END,item)
lb.pack()

# 与BROWSE相似 的为SINGLE,但不支持鼠标移动选中位置。
lb = Listbox(root, height=5, selectmode=SINGLE)
for item in ['python', 'tkinter', 'widget']:
    lb.insert(END, item)
lb.pack()


'''''4.使用selectmode  = EXPANDED使用Listbox来支持Shift和Control。'''
lb = Listbox(root, height=5,selectmode = EXTENDED)
for item in ['python','tkinter','widget']:
    lb.insert(END,item)
lb.pack()
#运行程序,点中“python",shift + 点击"widget",会选中所有的item
#运行程序,点中"python",control + 点击"widget",会选中python和widget,第二项tkinter处于非选中状态
'''''5.向Listbox中添加一个item'''
# 以上的例子均使用了insert来向Listbox中添加 一个item,这个函数有两个属性一个为添加的索引值,另一个为添加的项(item)
#  有两个特殊的值ACTIVE和END,ACTIVE是向当前选中的item前插入一个(即使用当前选中的索引作为插入位置);END是向
#  Listbox的最后一项添加插入一项
lb.insert(0,'linux','windows','unix')
lb.insert('end','end')
'''''6.删除Listbox中的项,使用delete,这个函数也有两个参数,第一个为开始的索引值; 
第二个为结束的索引值,如果不指定则只删除第一个索引项。'''
lb.delete(1,2)
'''''7.选中操作函数,使用函数实现。selection_set函数有两个参数第一个为开始的索引; 
第二个为结束的索引,如果不指定则只选中第一个参数指定的索引项'''
lb.selection_set(0,3)#选中条目
lb.selection_clear(0,3)  #取消选中
'''''8.得到当前Listbox中的item个数'''
print(lb.size()) #输出5
lb.pack()
'''''9.返回指定索引的项'''
print(lb.get(3))  #输出3
#get也为两个参数的函数,可以返回多个项(item),如下返回索引值3-7的值
print(lb.get(3,5))  #('3', '4', '5', '6', '7'),是一个tuple类型
'''''10.返回当前返回的项的索引,不是item的值'''
print(lb.curselection()) #(4, 5, 6, 7, 8, 9)
'''''11.判断 一个项是否被选中,使用索引。'''
lb.selection_set(0,3)#选中条目
print(lb.selection_includes(4))  #True
print(lb.selection_includes(0))  #False
'''''12.Listbox与事件绑定'''
def print_item(event):
    print(lb.get(lb.curselection()))
def print_item1(event):
    print(str(lb.get(lb.curselection()))+'双击了!')
lb.bind('<ButtonRelease-1>', print_item)#单击动作
lb.bind('<Double-Button-1>', print_item1)#双击动作

root.mainloop()

运行结果:

Tkinter教程之Listbox 列表部件

继续阅读
51changxue
  • 本文由 发表于 2018年4月7日 17:54:01
  • 转载请务必保留本文链接:https://51changxue.com/434.html
pycharm永久破解激活教程 Python

pycharm永久破解激活教程

PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试、语法高亮、Project管理、代码跳转、智能提示、自动完成、...
Tkinter控件快速引用总结 Python

Tkinter控件快速引用总结

  文件名称: Tkinter控件快速引用总结代码.rar 文件大小: 4k 更新日期: 2018-7-29 文件版本: V1.0 免责声明: 本站大部分下载资源收集于网络,只做学习和交流使...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定