程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python tkinter -- Chapter 14 Listbox Properties

編輯:Python

第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 屬性
常用的參數列表如下:

屬性描述activestyleThe style of the selected text:
underline: 下劃線
dotbox:Dotted line frame
none: 沒有任何的樣式background
bg背景顏色.默認是系統指定顏色borderwidth
bd邊框寬度.一般是1~2個像素值.cursorWhen the mouse moves over the list box,顯示的光標樣式disabledforegroundThe background color of the listbox when it is disabledexportselection默認是1.fontThe font of the text in the list box.只能選擇一種字體顯示.foreground
fg The color of the text in the list boxheightSets the height of the list box.默認值是10.highlightbackgroundWhen the listbox loses focus,邊框的高亮顏色highlightcolorwhen the listbox gets focus,邊框的高亮顏色highlightthickness設置高亮邊框的寬度listvariable設置listvariable屬性relief邊框的美化效果.默認值是SUNKEN,其他的可選項包括:FLAT, RAISED, GROOVE, and RIDGEselectbackgroundThe background color of the selected itemselectborderwidth選中的條目,It will be framed by a dashed rectangle.This parameter defines the width of the rectangle border.默認值是1.selectforegroundThe text color of the selected itemselectmode選擇模式:
MULTIPLE為多選
BROWSE:Selected by mouse movement
SINGLE:只能選一行.The mouse cannot be dragged to select.
EXTENDED:shift和ctrl配合使用takefocus標識用戶是否能夠使用Tab鍵移動焦點到標簽上.默認是空字符串,That is, the button can only be selected using the hotkey.state標簽的狀態,包括:NORMAL或者 DISABLEDwidthSets the width of the list box.默認值為20xscrollcommandAssociated with the scrollbar component(水平方向).使用方法可以參考:Scrollbar 組件yscrollcommandAssociated with the scrollbar component(垂直方向).使用方法可以參考:Scrollbar 組件14.1.1 activestyleSets the display effect of the selected text line in the list box.有三種:underline: 下劃線dotbox:Dotted dashed boxnone: 沒有顯示效果
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()

結果:


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved