14.2 方法
方法 | 描述 |
---|---|
activate(index) | 选中index代表的行 |
bbox | 获取选中条目的矩形包围框 |
curselection() | 获得当前选中的条目,返回值是一个列表。列表中的内容是选中的条目的索引值。 |
delete(first,last=none) | 删除条目,可以是1额或者多个,如listbox.delete(3,4),删除全部(0,END)。 First:要删除条目的起始索引 Last:要删除条目的结束索引。如果没有设定,只删除first指定的索引条目。 |
get(first,last=none) | 获得1个或者多个条目。具体参数参加delete(first,last=none) 该函数的返回值是一个字符串列表。 |
index(index) | 返回index指定的索引值。index可以是ACTIVE,也可以是ANCHOR,还可以是坐标值“@x,y”。 |
insert(index, *elements) | 追加项值,如listbox.insert(0,“addBox1”,“addBox2”)。 Index,为添加项值的位置。可以添加1个或者多个项值。 |
itemcget(index, option) | 获取列表框中,index指定项的配置属性。 |
itemconfig(index, **options) | 修改列表框中,index指定项的配置属性。 |
nearest(y) | 返回最靠近y坐标的索引值。 |
see(index) | 保证index指定的项值在列表框中可见。 |
selection_anchor(index) select_anchor(index) | 将tk.ANCHOR的值设为index。 |
selection_clear(first, last=None) select_clear(first, last=None) | 取消选中,比如listbox.select_clear(0,1)。参数first,last的解释,参照函数delete(first,last)。 |
selection_includes(index) select_includes(index) | 检查index指定的项目是否被选中。 |
selection_set(first, last=None) select_set(first, last=None) | 添加1个或者多个项目到被选中集合。 |
size() | 返回列表框的条目总数。 |
xview() | 控制水平滚动。 |
xview_moveto(fraction) | 控制水平滚动 |
xview_scroll(number, what) | 控制水平滚动number指定的位置。What参数指定移动的单位。可以是“units”,也可以是”pages” |
yview(*what) | 控制垂直滚动。 |
yview_moveto(fraction) | 控制垂直滚动 |
yview_scroll(number, what) | 垂直移动到number指定的位置。What参数指定移动的单位。可以是“units”,也可以是”pages” |
14.2.1 activate(index) | |
选中index代表的条目(行)。示例代码为选中一个条目,然后使用activate(3),此时选中的还是这个条目,不过下划线移动到了第四行。 |
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root)
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
def item():
b1.activate(3)
print(b1.curselection())
b2=tk.Button(root,text='Set',command=item)
b2.pack()
root.mainloop()
结果:
说明:首先选中任意一项,比如选中’8’,然后点击’set’按钮,这个时候激活了第四项,可以看到下划线移动到了4的下面。不过,此时调用.curselection(),得到的结果还是第八项被选中。
14.2.2 bbox(index)
返回指定行的矩形包围框。Index就是行号(从0开始计数的)。返回的是一个四元组(x,y,w,h)。x和y是左上角的坐标点,w和h分别表示宽度和长度。
bbox的用处是利用坐标点判断哪一行应该被选中。比如指定x,y坐标点,就可以判断该坐标点是否在某一行的矩形包围框内。
14.2.3 curselection
返回当前被选中的条目。返回值是一个元组,可以返回多个被选中的条目。
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,selectmode=tk.EXTENDED)
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
def item():
print(b1.curselection())
b2=tk.Button(root,text='Set',command=item)
b2.pack()
root.mainloop()
14.2.4 delete(first,last=None)
删除(first,last)之间的所有条目。如果没有定义last,则只删除first指定的条目。
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,selectmode=tk.EXTENDED)
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
def delete():
print(b1.delete(3,5))
b2=tk.Button(root,text='Delete',command=delete)
b2.pack()
root.mainloop()
结果:
14.2.5 get(first,last=none)
获取(first,last)指定范围内的所有条目信息。返回值是一个元组。
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,selectmode=tk.EXTENDED)
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
def get():
print(b1.get(3,5))
b2=tk.Button(root,text='Get',command=get)
b2.pack()
root.mainloop()
14.2.6 index(i)
返回index指定的数字化信息,也就是返回第几行(从0开始计数)
i 的取值包括:
(1)tk.ANCHOR: 此次选择的第一个条目所在行。如果支持多选,就算第一个被选中的条目已经取消了选择,也会返回该条目的行数。
(2)tk.ACTIVE:返回带下划线的条目。可能是被选择,也可能是被取消选择的。
(3)tk.END:最后一个选项的行数。不管是否被选择或者有操作。
(4)‘@x,y’:(x,y)是控件左上角为原点上的坐标点,单位是像素。返回值是最靠近该坐标点的行数
(5)行数:返回行数
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,selectmode=tk.EXTENDED)
for i in range(1,11):
b1.insert(tk.END,str(i))
b1.pack()
def index():
print(b1.index(tk.ANCHOR))
b2=tk.Button(root,text='Index',command=index)
b2.pack()
root.mainloop()
*14.2.7 insert(index, elements)
插入多个选项到列表框。
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,selectmode=tk.EXTENDED)
for i in range(1,11):
b1.insert(tk.END,str(i))
b1.pack()
def insert():
print(b1.insert(3,'23','24','25'))
b2=tk.Button(root,text='Insert',command=insert)
b2.pack()
root.mainloop()
结果:
14.2.8 itemcget(index, option)
获取index指定的列表项的属性。可以获取的属性见14.2.9节。
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,selectmode=tk.EXTENDED)
for i in range(1,11):
b1.insert(tk.END,str(i))
b1.pack()
b1.itemconfig(2,background='red')
def cget():
print(b1.itemcget(2,'background'))
b2=tk.Button(root,text='cget',command=cget)
b2.pack()
root.mainloop()
**14.2.9 itemconfig(index, options)
设置index指定的列表项的属性。属性值包括:
background: 背景颜色
forground: 文本颜色
selectbackground: 选择该列表项后的背景颜色
selectforeground: 选择该列表项后的文本颜色
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,selectmode=tk.EXTENDED)
for i in range(1,11):
b1.insert(tk.END,str(i))
b1.pack()
def config():
b1.itemconfig(2, background='blue')
b2=tk.Button(root,text='Config',command=config)
b2.pack()
root.mainloop()
结果:
14.2.10 nearest(y)
y是以列表框左上角为起始点的坐标值。nearest(y)返回的是最靠近坐标轴y的列表项索引,也就是序号或者行号,从0开始计数。
import tkinter as tk
root=tk.Tk()
root.geometry('320x240')
b1=tk.Listbox(root,selectmode=tk.EXTENDED,height=9)
for i in range(1,9):
b1.insert(tk.END,str(i))
b1.pack()
b3=tk.Entry(root,width=10)
b3.pack(side='left')
b4=tk.Label(root,text='结果: ')
b4.pack(side='right')
def nearest():
index=b1.nearest(b3.get())
b4['text']='结果:'+ str(index)
b2=tk.Button(root,text='Nearest',command=nearest)
b2.pack()
root.mainloop()
结果:
说明:y的坐标是200,那么离200最近的行是第8行(结果是7,需要加1)
14.2.11 see(index)
如果列表项很多,而列表框的高度有限,不能完全显示所有的列表项,除了使用滚动方法显示指定的列表项之外,还可以使用see(index)方法显示指定的列表项。index是列表项的行号。
import tkinter as tk
root=tk.Tk()
root.geometry('320x240')
b1=tk.Listbox(root,selectmode=tk.EXTENDED,height=9)
for i in range(1,300):
b1.insert(tk.END,str(i))
b1.pack()
b3=tk.Entry(root,width=10)
b3.pack(side='left')
def see():
b1.see(b3.get())
b2=tk.Button(root,text='See',command=see)
b2.pack()
root.mainloop()
结果:
说明:插入了300行。调用see(30)之后,第31行就出现在列表框的中间。
14.2.12 selection_anchor(index) 和select_anchor(index)
两个函数的作用是一样的。都是将tk.ANCHOR的值设定为index。tk.ANCHOR可以作为选择的起始索引或者其他用途。具体用法见14.2.15.
注:新版的tkinter使用selection_anchor()
14.2.13 selection_clear(first, last=None)和select_clear(first, last=None)
将(first,last)表示的列表项号/索引,从选中集合中去除。如果last没有输入,则只从选中集合中删除first一个列表项。
import tkinter as tk
root=tk.Tk()
root.geometry('320x240')
b1=tk.Listbox(root,selectmode=tk.EXTENDED,height=9)
for i in range(1,300):
b1.insert(tk.END,str(i))
b1.pack()
b3=tk.Entry(root,width=10)
b3.pack(side='left')
def clear():
b1.select_clear(b3.get())
b2=tk.Button(root,text='Clear',command=clear)
b2.pack()
root.mainloop()
结果:
14.2.14 selection_includes(index) 和 select_includes(index)
检测index指定的列表项是否在选中集合内。返回值是boolean类型。如果index在选中集合内返回True,否则返回False。
import tkinter as tk
root=tk.Tk()
root.geometry('320x240')
b1=tk.Listbox(root,selectmode=tk.EXTENDED,height=9)
for i in range(1,300):
b1.insert(tk.END,str(i))
b1.pack()
b3=tk.Entry(root,width=10)
b3.pack(side='left')
b4=tk.Label(root,text='结果:')
b4.pack(side='right')
def includes():
result=b1.selection_includes(b3.get())
b4['text']='结果:'+str(result)
b2=tk.Button(root,text='includes',command=includes)
b2.pack()
root.mainloop()
结果:
14.2.15 selection_set(first, last=None) 和 select_set(first, last=None)
添加(first,last)表示的列表项到选中集合中。
import tkinter as tk
root=tk.Tk()
root.geometry('320x240')
b1=tk.Listbox(root,selectmode=tk.EXTENDED,height=9)
for i in range(1,300):
b1.insert(tk.END,str(i))
b1.pack()
b3=tk.Entry(root,width=10)
b3.pack(side='left')
b4=tk.Label(root,text='结果:')
b4.pack(side='right')
def set1():
b1.selection_anchor(3)
b1.selection_set(tk.ANCHOR,b3.get())
result=b1.curselection()
b4['text']='结果:'+str(result)
b2=tk.Button(root,text='Set',command=set1)
b2.pack()
root.mainloop()
结果:
14.2.16 size()
返回列表项的总数。
import tkinter as tk
root=tk.Tk()
root.geometry('320x240')
b1=tk.Listbox(root,selectmode=tk.EXTENDED,height=9)
for i in range(1,300):
b1.insert(tk.END,str(i))
b1.pack()
b4=tk.Label(root,text=b1.size())
b4.pack()
root.mainloop()
结果:
14.2.17 xview、xview_moveto、xview_scroll
控制列表框的水平滚动。一般与水平滚动条一起使用。参见第十章的说明。
14.2.18 yview、yview_moveto、yview_scroll
控制列表框的垂直滚动。一般与垂直滚动条一起使用。参见第十章的说明。
更多推荐