tkinter模块Listbox操作

Python - Tkinter Listbox(列表框): 列表框部件用于显示一个项目列表,用户可以选择的项目数

 

列表框部件用于显示一个项目列表,用户可以选择的项目数.

语法:

这里是一个简单的语法来创建这个widget:

 w = Listbox ( master, option, ... )

参数:

  • master: 这代表了父窗口.

  • options: 下面是这个小工具最常用的选项列表。这些选项可以作为键 - 值对以逗号分隔.

OptionDescription
bgThe normal background color displayed behind the label and indicator.
bdThe size of the border around the indicator. Default is 2 pixels.
cursorThe cursor that appears when the mouse is over the listbox.
fontThe font used for the text in the listbox.
fgThe color used for the text in the listbox.
heightNumber of lines (not pixels!) shown in the listbox. Default is 10.
highlightcolorColor shown in the focus highlight when the widget has the focus.
highlightthicknessThickness of the focus highlight.
reliefSelects three-dimensional border shading effects. The default is SUNKEN.
selectbackgroundThe background color to use displaying selected text.
selectmode判断多少项目可以选择,鼠标拖动如何影响选择:
  • BROWSE: 通常情况下,你只能选择一条线,一个ListBox。如果您单击一个项目,然后拖动到不同的线路,选择将跟随鼠标。这是默认.
  • SINGLE: 你只能选择一条线,你可以拖不mouse.wherever的您单击按钮1,该行被选中.
  • MULTIPLE: 你可以选择任何的行数一次。点击任何行切换是否被选中.
  • EXTENDED: 你可以选择任何一次行相邻组的第一行上点击并拖动到最后一行.
widthThe width of the widget in characters. The default is 20.
xscrollcommandIf you want to allow the user to scroll the listbox horizontally, you can link your listbox widget to a horizontal scrollbar.
yscrollcommandIf you want to allow the user to scroll the listbox vertically, you can link your listbox widget to a vertical scrollbar.

方法:

ListBox的对象上的方法包括:

OptionDescription
activate ( index )Selects the line specifies by the given index.
curselection()返回一个包含选定的元素或元素的行号,从0开始计数。如果没有被选中,返回一个空的tuple.
delete ( first, last=None )Deletes the lines whose indices are in the range [first, last]. If the second argument is omitted, the single line with index first is deleted.
get ( first, last=None )Returns a tuple containing the text of the lines with indices from first to last, inclusive. If the second argument is omitted, returns the text of the line closest to first.
index ( i )If possible, positions the visible part of the listbox so that the line containing index i is at the top of the widget.
insert ( index, *elements )Insert one or more new lines into the listbox before the line specified by index. Use END as the first argument if you want to add new lines to the end of the listbox.
nearest ( y )Return the index of the visible line closest to the y-coordinate y relative to the listbox widget.
see ( index )Adjust the position of the listbox so that the line referred to by index is visible.
size()Returns the number of lines in the listbox.
xview()To make the listbox horizontally scrollable, set the command option of the associated horizontal scrollbar to this method.
xview_moveto ( fraction )Scroll the listbox so that the leftmost fraction of the width of its longest line is outside the left side of the listbox. Fraction is in the range [0,1].
xview_scroll ( number, what )Scrolls the listbox horizontally. For the what argument, use either UNITS to scroll by characters, or PAGES to scroll by pages, that is, by the width of the listbox. The number argument tells how many to scroll.
yview()To make the listbox vertically scrollable, set the command option of the associated vertical scrollbar to this method.
yview_moveto ( fraction )Scroll the listbox so that the top fraction of the width of its longest line is outside the left side of the listbox. Fraction is in the range [0,1].
yview_scroll ( number, what )Scrolls the listbox vertically. For the what argument, use either UNITS to scroll by lines, or PAGES to scroll by pages, that is, by the height of the listbox. The number argument tells how many to scroll.

例子:

自行尝试下面的例子:

from Tkinter import *
import tkMessageBox
import Tkinter

top = Tk()

Lb1 = Listbox(top)
Lb1.insert(1, "Python")
Lb1.insert(2, "Perl")
Lb1.insert(3, "C")
Lb1.insert(4, "PHP")
Lb1.insert(5, "JSP")
Lb1.insert(6, "Ruby")

Lb1.pack()
top.mainloop()

这将产生以下结果:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值