第14章 列表框(Listbox)
Listbox controls display multiple lines of text,The user can select one or more rows.Use only one font for all text,Multiple fonts cannot be mixed.
14.1 屬性
常用的參數列表如下:
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,activestyle='dotbox')
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
root.mainloop()
結果:
14.1.2 background(bg)
Sets the listbox background color.
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,bg='green')
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
root.mainloop()
結果:
14.1.3 borderwidth(bd)
Sets the border width of the list box.
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,bd=10)
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
root.mainloop()
結果:
14.1.4 cursor
When the mouse is in the list box area,鼠標的形狀.詳細的cursor說明見3.3.6節.
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,cursor='spider')
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
root.mainloop()
14.1.5 disabledforeground
The state of the listbox is tk.DISABLED時,The text color of the list box.
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,disabledforeground='red')
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
b1.config(state=tk.DISABLED)
root.mainloop()
結果:
14.1.6 exportselection
Determines whether the selected text content can be copied.如果exportselection=True,表示可以.exportselection=False,Indicates the selected option in the non-copyable list box.
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,exportselection=0)
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
root.mainloop()
14.1.7 font
Sets the list box font.All text can only have one font,Multiple fonts cannot be mixed.See the specific text description3.3.3節.
14.1.8 foreground(fg)
Sets the text color in the list box.
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,fg='blue')
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
root.mainloop()
結果:
14.1.9 height
Sets the height of the list box.單位是行.
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,height=5)
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
root.mainloop()
結果:
Sets the height of the list box.默認是10行.
14.1.10 highlightbackground、highlightcolor和highlightthickness
Sets the border color of the list box when it gains or loses input focus.The width of the two borders is determined by highlightthickness設置.
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,highlightbackground='blue',
highlightcolor='red',highlightthickness=10)
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
root.mainloop()
結果:
14.1.11 listvariable
listvariable 可以與一個tk.StringVar()變量相關聯.through variablesget()方法,Get all the text content in the list box.也可以通過set(s)method to set the contents of the list box.
#Get the contents of the listbox
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
content=tk.StringVar()
b1=tk.Listbox(root,listvariable=content)
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
print(content.get())
root.mainloop()
#Sets the contents of the list box
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
content=tk.StringVar()
b1=tk.Listbox(root,listvariable=content)
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
def lst():
content.set('abc def "c c" c bbb')
b2=tk.Button(root,text='Set',command=lst)
b2.pack()
root.mainloop()
結果:
說明:
(1)使用set(s)will replace the previous list box contents
(2)sis a space-separated string.Multiple spaces will be considered1個.If spaces are required in options,Please enclose this string in quotation marks.比如”c c”.
14.1.12 relief
Sets the border of the list box3D效果.見3.3.5節的說明
14.1.13 selectbackground
The background color of the selected item.The default value is blue.
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,selectbackground='red')
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
root.mainloop()
結果:
14.1.14 selectborderwidth
Indicates the border width of the selected rectangle.
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,selectborderwidth=5)
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
root.mainloop()
結果:
說明:It can be seen that the spacing is significantly better than not setselectborderwidth加大了.
14.1.15 selectforeground
The selected text color.It is currently displayed in reverse white,也可以通過selectforeground來設定.
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,selectforeground='red')
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
root.mainloop()
結果:
14.1.16 selectmode
Sets the selection mode for items in the list box:
(1)tk.BROWSE: Items can be selected by dragging with the mouse.Only one row can be selected at a time
(2)tk.SINGLE: Only one entry can be selected at a time.Mouse drag mode selection is not supported.
(3)tk.MULTIPLE: Multiple entries can be selected.if the entry is already selected,Click again to make it unchecked
(4)tk.EXTENDED :The following mode selections are supported:
拖動選擇.鼠標拖動,Items over the mouse are selected
Shift:Click the mouse to select an item,然後按照shift鍵,Mouse click on another entry,則這2All entries between entries are selected
Ctrl: 按住ctrl鍵,Click on an item at the same time,then the entry is selected.Only one selected item can be added at a time.if the entry is already selected,The state becomes unselected.
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Listbox(root,selectmode=tk.SINGLE)
for i in range(1,11):
b1.insert(tk.END,i)
b1.pack()
root.mainloop()
14.1.17 takefocus
設置是否可以通過TabMove the input focus to the list box.
14.1.18 state
Sets the state of the list box.有二種:DISABLED或者NORMAL.
14.1.19 width
Sets the width of the list box.默認是20個字符.
14.1.20 xscrollcommand
設置水平滾動條.詳細的用法見yscrollcommand.
14.1.21 yscrollcommand
設置垂直滾動條.
import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
content=tk.StringVar()
content.set('1 2 3 jjjjjjjjjjjjjjjjjjjjjjjjjjj\
jjjjjjjjjjjjjj 4 5 6 7 8 9 10')
f=tk.Frame(root)
s1 = tk.Scrollbar(f,orient=tk.HORIZONTAL)
s2 = tk.Scrollbar(f,orient=tk.VERTICAL)
b1 = tk.Listbox(f,width=10,height=5,listvariable=content,
xscrollcommand=s1.set,yscrollcommand=s2.set)
s1.pack(side=tk.BOTTOM,fill=tk.X)
s1.config(command=b1.xview)
s2.pack(side=tk.RIGHT,fill=tk.Y)
s2.config(command=b1.yview)
b1.pack()
f.pack()
root.mainloop()
結果: