Listbox

本文详细介绍了 Tkinter 中 Listbox 组件的各种配置选项及其使用方法,包括不同选择模式的特点及操作方式,并提供了插入、删除及获取列表项的具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

-background => color
Sets the color of the area behind the text. -borderwidth => amount
Sets the width of the edges of the widget. Default is 2. -cursor => cursorname
Sets the cursor to display when the mouse is over the Listbox.
-exportselection => 0 | 1
Determines if the current Listbox selection is made available for the X selection as well. If set to 1, prevents two Listboxes from having selections at the same time.
-font => fontname
Sets the font of any text displayed within the Listbox.
-foreground => color
Sets the color of nonselected text displayed in the Listbox.
-height => amount
Sets the height of the Listbox.
-highlightbackground => color
Sets the color the highlight rectangle should be when the Listbox does not have the keyboard focus.
-highlightcolor => color
Sets the color the highlight rectangle should be when the Listbox does have the keyboard focus.
-highlightthickness => amount
Sets the thickness of the highlight rectangle. Default is 2.
-relief => 'flat'|'groove'|'raised'|'ridge'|' sunken '|'solid'
Sets the relief of the edges of the Listbox.
-selectbackground => color
Sets the color behind any selected text.
-selectborderwidth => amount
Sets the width of the border around any selected text.
-selectforeground => color
Sets the color of the text in any selected items.
-selectmode => "single" | " browse " | "multiple" | "extended"
Affects how many items can be selected at once; also affects some key/mouse bindings for the Listbox (such as Shift-select).
-setgrid => 0 | 1
Turns gridding off or on for the Listbox. Default is 0.
-takefocus => 0 | 1 | undef
Determines whether the widget can have keyboard focus. 0 means never, 1 means always, undef means dynamic decision.
-width => amount
Sets the width of the Listbox in characters. If amount is 0 or less, the Listbox is made as wide as the longest item.
-xscrollcommand => callback
Assigns a horizontal Scrollbar to widget. 
-yscrollcommand => callback
Assigns a vertical Scrollbar to widget. 


Selection Mode:

The possible select modes are "browse" , "single" , "multiple" , or "extended" . The default mode is "browse" .

browse and single
These modes are similar in that only one item can be selected at a time; clicking on any item will deselect any other selection in the Listbox. The browse mode has a slight difference: when the mouse is held down and moving around, the selection moves with the mouse. For bind purposes, a "<Button-1>" bind will be invoked when you first click down. If you want to catch the event when the mouse is released, define a ButtonRelease binding.
extended
This mode lets you select more than one item at a time. Clicking on a single item with the left mouse button will deselect any other selection, but you can Shift-click or Control-click to add more items to your selection. Shift-clicking (holding down the Shift key while pressing a mouse button) will extend the selection from the already selected item to the newly selected item. Control-clicking (holding down the Control key while pressing a mouse button) will add the item being clicked to the selection, but it won't alter any of the other selections. You can also click an item with the mouse button, hold down the button, and then move the pointer over other items to select them. This is what's called a click-drag motion. Using "extended" allows for very fast selection of many different items in the Listbox.
multiple
This mode also allows you to select more than one item. Instead of Shift-clicking or Control-clicking, you select items one at a time. Clicking on an unselected item will select it, and clicking on an already selected item will unselect it.

Insert Item:
$lb->insert(index

, element
, element


 ... );
index 0 和end 分别代表最前面和最后面
删除item:
$lb->delete(firstindex

 [, lastindex ]


);

$lb->delete(0, 'end');删除所有的item

获取item:

The get method returns a list of Listbox elements specified by the indexes firstindex to lastindex :

$lb->get(firstindex

 [,  lastindex ]


);

If only the firstindex is specified, only one element is returned. The firstindex must be less than or equal to the lastindex . To get a list of all elements in the Listbox:

@elements = $lb->get(0, 'end');

To get the last item in the Listbox:

$lastitem = $lb->get('end');

To find out which items in the Listbox are selected, use the curselection method:

@list = $lb->curselection();

$lb->delete(firstindex

 [, lastindex ]


);

获取item大小:
$count = $lb->size( );
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惹不起的程咬金

来都来了,不赏点银子么

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值