欢迎光临
我们一起进阶

Python的GUI编程(七)Listbox(列表框)

扫码或搜索:沉默王二
发送 290992
即可立即永久解锁本站全部文章
Listbox:列表框用于显示项目列表
语法:语法:

lb=List(master,option,···)

参数  master:父窗口

   option:常用的选项列表,可以有多个,用逗号隔开

可以通过for循环向Listbox中插入项目列表和索引值向其中添加项目两种方法。
selectmode

确定可以选择多少项,以及鼠标拖动的影响选择:

  • BROWSE:通常,只能从列表框中选择一行。如果单击一个项目,然后拖动到不同的行,选择将会跟随鼠标,是默认的。

  • SINGLE:你只能选择一行,不能拖动。

  • MULT IPLE:您可以同时选择任意数量的行。点击在任意直线上,无论它是否被选中。不能拖动。

  • EXTENDED: 您可以一次选择任何相邻的g线。单击第一行并将g拖到最后一行。能拖动

使用selectmode = EXPANDED使用Listbox来支持Shift和Control(如Windows下的快捷键)

from Tkinter import *
root=Tk()

#单选
LB1=Listbox(root)

Label(root,text='单选:选择你的课程').pack()
for item in ['Chinese','English','Math']:
    LB1.insert(END,item)
LB1.pack()

#多选
LB2=Listbox(root,selectmode=MULTIPLE)
Label(root,text='多选:你会几种编程语言').pack()
for item in ['python','C++','C','Java','Php']:
    LB2.insert(END,item)
LB2.pack()

root.mainloop()

insert()也可以选择在已有的item前后插入新的item,只需要指出在第几个。

删除Listbox中的项,使用delete,这个函数也有两个参数,第一个为开始的索引值;第二个为结束的索引值,如果不指定则只删除第一个索引项。

删除全部内容,使用delete指定第一个索引值0和最后一个参数END,即可

  1. selection_set()指定选择的条目

  2. selection_clear()取消选择的条目

  3. size():得到条目个数

  4. get():得到索引的条目

  5. selection_includes()判断一个项是否被选中

示例:

from Tkinter import *
root=Tk()

#单选
LB1=Listbox(root)

Label(root,text='单选:选择你的课程').pack()
for item in ['Chinese','English','Math']:
    LB1.insert(END,item)
LB1.pack()

#多选
LB2=Listbox(root,selectmode=EXTENDED)
Label(root,text='多选:你会几种编程语言').pack()
for item in ['python','C++','C','Java','Php']:
    LB2.insert(END,item)
LB2.insert(1,'JS','Go','R')
LB2.delete(5,6)
LB2.select_set(0,3)
LB2.select_clear(0,1)
print LB2.size()
print LB2.get(3)
print LB2.select_includes(3)
LB2.pack()

root.mainloop()

结果:

6
R
True

Listbox与变量绑定
from Tkinter import *
root = Tk()
v = StringVar()
lb = Listbox(root,listvariable = v)
for i in range(10):
    lb.insert(END,str(i*100))
#打印当前列表中的项值
print v.get()
#输出:('0', '100', '200', '300', '400', '500', '600', '700', '800', '900')
#改变v的值,使用tuple可以与item对应
v.set(('1000','200'))
#结果只有两项了1000和200
lb.pack()
root.mainloop()

Listbox与事件绑定

它不支持command属性来设置回调函数了,使用bind来指定回调函数,打印当前选中的值
绑定函数:

def bind(self, sequence=None, func=None, add=None):
    """Bind to this widget at event SEQUENCE a call to function FUNC.

SEQUENCE is a string of concatenated event
patterns. An event pattern is of the form
where MODIFIER is one
of Control, Mod2, M2, Shift, Mod3, M3, Lock, Mod4, M4,
Button1, B1, Mod5, M5 Button2, B2, Meta, M, Button3,
B3, Alt, Button4, B4, Double, Button5, B5 Triple,
Mod1, M1. TYPE is one of Activate, Enter, Map,
ButtonPress, Button, Expose, Motion, ButtonRelease
FocusIn, MouseWheel, Circulate, FocusOut, Property,
Colormap, Gravity Reparent, Configure, KeyPress, Key,
Unmap, Deactivate, KeyRelease Visibility, Destroy,
Leave and DETAIL is the button number for ButtonPress,
ButtonRelease and DETAIL is the Keysym for KeyPress and
KeyRelease. Examples are
for pressing Control and mouse button 1 or
for pressing A and the Alt key (KeyPress can be omitted).
An event pattern can also be a virtual event of the form
<> where AString can be arbitrary. This
event can be generated by event_generate.
If events are concatenated they must appear shortly
after each other.

FUNC will be called if the event sequence occurs with an
instance of Event as argument. If the return value of FUNC is
“break” no further bound function is invoked.

An additional boolean parameter ADD specifies whether FUNC will
be called additionally to the other bound function or whether
it will replace the previous function.

Bind will return an identifier to allow deletion of the bound function with
unbind without memory leak.

If FUNC or SEQUENCE is omitted the bound function or list
of bound events are returned.”””

return self._bind((‘bind’, self._w), sequence, func, add)

from Tkinter import *
root = Tk()
def CallOn(event):
    root1=Tk()
    Label(root1,text='你的选择是'+lb.get(lb.curselection())+"语言!").pack()
    Button(root1,text='退出',command=root1.destroy).pack()
lb = Listbox(root)
#双击命令
lb.bind('<Double-Button-1>',CallOn)
for i in ['python','C++','C','Java','Php']:
    lb.insert(END,i)
lb.insert(END,'JS','Go','R')
lb.pack()
root.mainloop()

参考:http://blog.csdn.net/jcodeer/article/details/1811310

代码装饰:通过Button将选中的Listbox中的条目打印在标签Label和Entry中

from Tkinter import *
root = Tk()
root.geometry('100x100')

lb = Listbox(root)
#双击命令
v=StringVar()

#Entry类似文本框,可只读,也可修改,还可通过回调函数获取文本
#Label标签超做不是那么灵活但是也可通过回调函数获取文本
Entry(root,textvariable=v,width=6).pack()
Label(root,textvariable=v,width=6,bg='green').pack()
def Call_Entry():
    val=lb.get(lb.curselection())
    v.set(val)

Button(root,text='press',fg='blue',command=Call_Entry,width=5).pack()

for i in ['python','C++','C','Java','Php']:
    lb.insert(END,i)
lb.insert(END,'JS','Go','R')
lb.pack()
root.mainloop()

————————————————
版权声明:本文为CSDN博主「痴情一笑恋红颜」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_37264397/article/details/79079259

赞(1) 打赏
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

小白学堂,学的不止是技术,更是前程

关于我们免责声明

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏