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

语法:

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

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

参数:

  • master: 这代表了父窗口.

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

Option Description
bg The normal background color displayed behind the label and indicator.
bd The size of the border around the indicator. Default is 2 pixels.
cursor The cursor that appears when the mouse is over the listbox.
font The font used for the text in the listbox.
fg The color used for the text in the listbox.
height Number of lines (not pixels!) shown in the listbox. Default is 10.
highlightcolor Color shown in the focus highlight when the widget has the focus.
highlightthickness Thickness of the focus highlight.
relief Selects three-dimensional border shading effects. The default is SUNKEN.
selectbackground The background color to use displaying selected text.
selectmode 判断多少项目可以选择,鼠标拖动如何影响选择:

  • BROWSE: 通常情况下,你只能选择一条线,一个ListBox。如果您单击一个项目,然后拖动到不同的线路,选择将跟随鼠标。这是默认.
  • SINGLE: 你只能选择一条线,你可以拖不mouse.wherever的您单击按钮1,该行被选中.
  • MULTIPLE: 你可以选择任何的行数一次。点击任何行切换是否被选中.
  • EXTENDED: 你可以选择任何一次行相邻组的第一行上点击并拖动到最后一行.
width The width of the widget in characters. The default is 20.
xscrollcommand If you want to allow the user to scroll the listbox horizontally, you can link your listbox widget to a horizontal scrollbar.
yscrollcommand If you want to allow the user to scroll the listbox vertically, you can link your listbox widget to a vertical scrollbar.

方法:

ListBox的对象上的方法包括:

Option Description
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()

这将产生以下结果:

Tkinter Listbox(列表框)的更多相关文章

  1. [tkinter]为列表框添加滚动条

    为了给列表框配备滚动条,看来很多别人的博客 终于解决了问题 ,现在我总结一下 from tkinter import * root = Tk() lb = Listbox(root) scr = Sc ...

  2. MFC编程入门之二十四(常用控件:列表框控件ListBox)

    前面两节讲了比较常用的按钮控件,并通过按钮控件实例说明了具体用法.本文要讲的是列表框控件(ListBox)及其使用实例. 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选 ...

  3. VS2010/MFC编程入门之二十四(常用控件:列表框控件ListBox)

    前面两节讲了比较常用的按钮控件,并通过按钮控件实例说明了具体用法.本文要讲的是列表框控件(ListBox)及其使用实例. 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选 ...

  4. tkinter窗口系列之一——列表框

    以下内容来自https://tkdocs.com/tutorial/morewidgets.html 一个列表框显示由单行文本所组成的一栏条目,通常它很冗长,它允许使用者通过列表浏览其中的内容,选择一 ...

  5. Spring MVC-表单(Form)标签-列表框(Listbox)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_listbox.htm 说明:示例基于Spring MVC 4.1.6. 以下示例 ...

  6. (七)对话框,单选框(radiobox),复选框(checkbox),列表框(ListBox),组合框(CComboBox),水平滚动条(Horizontal scroll bar),微调(旋转)spincontrol,列表视图控件CListCtrl,静态控件static

    1,模态对话框和非模态对话框 // 模态对话框 void CMainFrame::OnDialogExec() { // TODO: 在此添加命令处理程序代码 // 创建对话框对象 CDialog d ...

  7. VS2010-MFC(常用控件:列表框控件ListBox)

    转自:http://www.jizhuomi.com/software/186.html 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选中的项会高亮显示.列表框可分为单 ...

  8. 列表框List Box

    List Box/Check List Box ListBox窗口用来列出一系列的文本,每条文本占一行.创建一个列表窗口可以使用成员函数: BOOL CListBox::Create( LPCTSTR ...

  9. CIconListBox带图标的列表框类

    有时候,我们需要在列表框ListBox中插入带图标的文字项,这就需要自己派生一个类出来了,网上的一个CIconListBox类还不错,网站http://www.codeguru.com/Cpp/con ...

随机推荐

  1. idea和pycharm激活方法

    idea和pycharm是jetbrains出品的很火的开发辅助工具,不过目前来说是收费的,对于囊中羞涩的同学来说还是比较困难的 我写这篇博客只是为了方便新手使用,推进开发进程,但希望大家在合适的情况 ...

  2. android 获取 图片或视频略缩图

    /** * 根据指定的图像路径和大小来获取缩略图 此方法有两点好处: 1. * 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度, * 第二次读取的bitmap是根 ...

  3. mysql 时间转换 用EXCEL实现MySQL时间戳格式和日期格互转

    今天项目表中需要导入好几w条数据 ,但日期由两个一个是标准时间一个为时间戳,程序中搜索是根据时间戳来搜索的,所以在网上翻箱倒柜的终于找到解决之道了,利用excel转换时间戳 时间戳转成正常日期的公式: ...

  4. 深度优先搜索入门:POJ1164城堡问题(递归、用栈模拟递归)

    将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为:Dfs(v) {if( v 访问过)return;将v标记为访问过;对和v相邻的每个点u: Dfs(u);}int main ...

  5. LOJ107. 维护全序集【树状数组维护全序集】

    题目描述 这是一道模板题,其数据比「普通平衡树」更强. 如未特别说明,以下所有数据均为整数. 维护一个多重集 S ,初始为空,有以下几种操作: 把 x 加入 S 删除 S 中的一个 x,保证删除的 x ...

  6. 常用输入法快速输入自定义格式的时间和日期(搜狗/QQ/微软拼音)

    几个主流的输入法输入 rq 或者 sj 都可以得到预定义格式的日期或者时间.然而他们都是预定义的格式:当我们需要一些其他格式的时候该怎么做呢? 本文将介绍几个常用输入法自定义时间和日期格式的方法. 主 ...

  7. 淘宝npm镜像使用

    淘宝NPM镜像使用 镜像使用方法 1.config命令 npm config set registry https://registry.npm.taobao.org npm info undersc ...

  8. spring事务传播机制的测试结果

    /**     * @Component是个一般性的注解,使用此注解修饰的POJO类,有value属性,指定bean的id.也可不写.默认值是类名首字母小写     * @Resource是控制依赖注 ...

  9. ArangoDB Foxx service 使用

    备注:   项目使用的是github https://github.com/arangodb-foxx/demo-hello-foxx 1. git clone git clone https://g ...

  10. 什么是spark(二) RDD

    其实你会发现很多概念都是基于RDD提出来的,比如分区,缓存这些操作的对象其实都是RDD:所以不要讲spark的分区,这其实很不专业,分区其实是属于RDD的概念(只有pair RDD才有分区概念) RD ...