python tkinter Listbox用法

python tkinter组件的Listbox的用法,见下面代码的演示:

 1 from tkinter import *
 2
 3 root=Tk()
 4 v=StringVar()
 5 #Listbox与变量绑定‘
 6 lb1=Listbox(root,listvariable=v)
 7 v.set((‘10‘,‘20‘,‘30‘,‘40‘,‘50‘))
 8 print(v.get())
 9 lb1.pack()
10
11 #.创建一个可以多选的Listbox,使用属性selectmaod‘
12 lb2=Listbox(root,selectmode=MULTIPLE)
13 for item in range(10):
14     lb2.insert(END,str(item))
15 #  有两个特殊的值ACTIVE和END,ACTIVE是向当前选中的item前插入一个
16 # (即使用当前选中的索引作为插入位置);END是向
17 #  Listbox的最后一项添加插入一项
18
19 lb2.delete(1,3)
20 #删除全部内容,使用delete指定第一个索引值0和最后一个参数END,即可
21 lb2.pack()
22
23 #这个属性selectmode还可以设置为BROWSE,可以通过鼠标来移动Listbox中的选中位置
24 # (不是移动item),
25 #这个属性也是Listbox在默认设置的值,这个程序与1.程序运行的结果的一样的
26 scrollbar=Scrollbar(root)
27 scrollbar.pack(side=RIGHT,fill=Y)
28 lb3=Listbox(root,selectmode=BROWSE,yscrollcommand=scrollbar.set)
29 for item in range(20):
30     lb3.insert(END,str(item))
31     lb3.pack(side=LEFT,fill=BOTH)
32 scrollbar.config(command=lb3.yview)
33
34 #将一个垂直方向的Scrollbar和listboxs/canvases/text fields这些控件结合起来,
35 # 你只需要按照下面的步骤即可:
36 #1.将这些控件的yscrollcommand选项设置为scrollbar的set方法。
37 #2.将scrollbar的command选项设置为这些控件的yview方法。
38
39 print(lb3.size())
40 print(lb3.get(3))
41 print(lb3.get(3,7))
42 lb3.selection_set(0,10)
43 lb3.selection_clear(0,3)
44 lb3.pack()
45
46
47 def printlist(event):
48     print(lb4.get(lb4.curselection()))
49
50
51 #使用selectmode  = EXPANDED使用Listbox来支持Shift和Control
52 lb4=Listbox(root,selectmode=EXTENDED)
53 lb4.bind(‘<Double-Button-1>‘,printlist)
54 for item in [‘python‘,‘tkinter‘,‘widget‘]:
55     lb4.insert(END,item)
56 lb4.pack()
57
58
59 root.mainloop()

原文地址:https://www.cnblogs.com/hackpig/p/8146518.html

时间: 2024-08-11 20:21:44

python tkinter Listbox用法的相关文章

Python Tkinter 插件用法讲解(一) (python2.7)

本文是自己总结的从网上学到的东西 一.什么是Tkinter?    Tkinter是python内置的GUI图形库,支持多个操作系统,使用Tcl语言开发: 我们编写的Python代码会调用内置的Tkinter,Tkinter封装了访问Tk的接口: Tk会调用操作系统提供的本地GUI接口,完成最终的GUI 二.Tkinter模块的使用 1)from Tkinter import * 2)import Tkinter     两者的使用是有差别的 我所遇到的问题是如下代码: <span style=

Python Tkinter基础控件入门实例

分享一个Python Tkinter基础控件用法的入门例子,包括窗口的显示.显示内置图片.弹出窗口.菜单等. 例子,Python Tkinter基础控件的用法 # -*- coding: utf-8 -*- from Tkinter import * def btn_click(): b2['text'] = 'clicked' evalue = e.get() print 'btn Click and Entry value is %s' % evalue def btn_click_bind

python &#160; __name__==&#39;__main__&#39; 用法

python 文件的后缀为.py,比如 name.py python 文件可以直接执行,也可以被导入.调用,比如import name; script 1: #!/bin/python # Filename:name.py if __name__=='__main__':     print 'This program is being run by itself' else:     print 'I am being imported from another module' script

python之函数用法capitalize()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法capitalize() #capitalize() #说明:将字符串的第一个字母变成大写,其他字母变小写. ''' capitalize(...) S.capitalize() -> string Return a copy of the string S with only its first character capitalized. ''' #案例 str='xiaoden

python之函数用法setdefault()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法setdefault() #D.get(k,d) #说明:k在D中,则返回 D[K],如果k不在D中,则返回d值 #D.get(k,d), also set D[k]=d if k not in D ''' >>> help(dict.setdefault) Help on built-in function setdefault: setdefault(...) D.set

python之函数用法islower()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法islower() #http://www.runoob.com/python/att-string-islower.html #islower() #说明:检测字符串是否都由小写字母组成 str = "THIS is string example....wow!!!" print str.islower()#False str = "this is string

python之函数用法xrange()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法xrange() #xrange() #说明:返回一个生成器 #xrange做循环的性能比range好,尤其是返回很大的时候.除非要返回一个列表,则用range. ''' class xrange(object) | xrange(stop) -> xrange object | xrange(start, stop[, step]) -> xrange object | | Li

python之函数用法startswith()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法startswith() #http://www.runoob.com/python/att-string-startswith.html #startswith() #说明:返回布尔值,用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False. ''' startswith(...) S.startswith(prefix[, start[, end]]

python之函数用法globals()

# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法globals() #globals() #说明:在当前作用域下,查看全局变量 ''' globals(...) globals() -> dictionary Return the dictionary containing the current scope's global variables. ''' #案例 b='xiaodeng' print globals#<buil