立即学习:https://edu.csdn.net/course/play/19711/343113?utm_source=blogtoedu

listbox

知识点:

1)创建listbox:

self.item_listbox = tkinter.Listbox(self.root,selectmode = "multiple", font=("微软雅黑",12),fg = "black",bg = "white")#设置一个listbox,且为复选框(multiple),单选框(single)

2)设置复选还是单选:selectmode = "multiple"

3)获得当前选择项的索引:

self.item_listbox.curselection()#返回的是一个列表,获得当前listbox中被选中第一个元素的索引

4)根据索引获得项目内容

self.chosen_listbox.insert('end',self.item_listbox.get(self.chosen_index))#根据索引获得元素的内容

完整代码:

'''
设计一个简单的选择listbox,由两个listbox和两个label以及一个按钮组成,
将左边选中的元素,通过点击按钮或者双击元素,自动添加到右边的listbox中
'''
from tkinter import *
import tkinterclass mainwindow():def __init__(self):#------------创建主窗体-----------------self.root = tkinter.Tk()self.root.title("linlianqin")self.root.geometry('450x280')  # 定义窗体的初始大小self.root.maxsize(1200, 1200)  # 设置窗口可以显示的最大尺寸self.item_list()self.chosen_button()self.chosen_list()self.root.mainloop()  # 显示窗口,这个代码一定要放在所有窗口设置的后面def item_list(self):#定义列表1self.items = ["python","c","java","PHP"]#设置一个listbox元素组成的列表self.item_label = tkinter.Label(self.root,text = "请选择你感兴趣的语言:",font = ("微软雅黑",9),fg = "white",bg = "#123333")self.item_label.grid(row = 0,column = 0)self.item_listbox = tkinter.Listbox(self.root,selectmode = "multiple", font=("微软雅黑",12),fg = "black",bg = "white")#设置一个listbox,且为复选框(multiple),单选框(single)#将元素插入到listbox当中for item in self.items:self.item_listbox.insert("end",item)#定义双击选中元素自动添加到另一个列表的事件self.item_listbox.bind("<Double-Button-1>",self.button_multiple_event)self.item_listbox.grid(row = 1,column = 0)def chosen_button(self):#定义一个添加按钮self.chosenbutton = tkinter.Button(self.root,text = "add>>>",font = ("微软雅黑",12),fg = "black",bg = "#fffffe")self.chosenbutton.bind("<Button-1>",self.button_multiple_event)self.chosenbutton.grid(row = 1,column = 1)def chosen_list(self):#定义另一个列表,用于存放选中的元素self.chosen_label = tkinter.Label(self.root, text="感兴趣的语言:", font=("微软雅黑", 9),fg="white", bg="#123393")self.chosen_label.grid(row=0, column=2)self.chosen_listbox = tkinter.Listbox(self.root,  font=("微软雅黑", 12),fg="black", bg="white")  # 设置一个listboxself.chosen_listbox.grid(row=1,column=2)def button_single_event(self,event):#定义按钮单选事件self.chosen_index = self.item_listbox.curselection()[0]#获得当前listbox中被选中第一个元素的索引# self.item_listbox.curselection()返回的是一个列表self.chosen_listbox.insert('end',self.item_listbox.get(self.chosen_index))#根据索引获得元素的内容def button_multiple_event(self,event):#定义按钮复选事件for self.chosen_index in self.item_listbox.curselection():self.chosen_listbox.insert('end',self.item_listbox.get(self.chosen_index))#若选中了就删除该项while True:if self.item_listbox.curselection():#如果当前由被选择中的项目self.item_listbox.delete(self.item_listbox.curselection()[0])else:breakdef main():mainwindow()if __name__ == '__main__':main()

学习笔记(51):Python实战编程-ListBox相关推荐

  1. 学习笔记(58):Python实战编程-Combobox

    立即学习:https://edu.csdn.net/course/play/19711/343121?utm_source=blogtoedu 1.下拉列表Combobox:与Listbox相比,下拉 ...

  2. 学习笔记(55):Python实战编程-Scrollbar

    立即学习:https://edu.csdn.net/course/play/19711/343118?utm_source=blogtoedu 1.滚动条ScrollBar: 当列表内容项的内容过多时 ...

  3. 学习笔记(53):Python实战编程-Checkbutton

    立即学习:https://edu.csdn.net/course/play/19711/343116?utm_source=blogtoedu 复选框Checkbutton:与单选框是相对的,一些用法 ...

  4. 学习笔记(57):Python实战编程-Treeview

    立即学习:https://edu.csdn.net/course/play/19711/343120?utm_source=blogtoedu 1.树状结构Treeview:分为树状折叠式列表和列表显 ...

  5. 学习笔记(54):Python实战编程-Scale

    立即学习:https://edu.csdn.net/course/play/19711/343117?utm_source=blogtoedu 1.滑块组件Scale: 用于定义一定范围的区间,如音量 ...

  6. 学习笔记(52):Python实战编程-Radiobutton

    立即学习:https://edu.csdn.net/course/play/19711/343115?utm_source=blogtoedu 单选钮:Radiobutton 1)相对于大部分组件而言 ...

  7. 学习笔记(49):Python实战编程-place布局

    立即学习:https://edu.csdn.net/course/play/19711/343111?utm_source=blogtoedu 1.place布局: 1)最灵活的布局方式,是根据坐标点 ...

  8. 学习笔记(48):Python实战编程-grid布局

    立即学习:https://edu.csdn.net/course/play/19711/343110?utm_source=blogtoedu grid布局:根据表结构进行的布局,索引为0行0列开始, ...

  9. 学习笔记(47):Python实战编程-pack布局

    立即学习:https://edu.csdn.net/course/play/19711/343109?utm_source=blogtoedu pack布局: 1)最常用的布局,顺序排列布局方法 2) ...

最新文章

  1. 华为dra路由方式分为relay和proxy_华为云计算(3)——网络虚拟化
  2. 使用MySQL验证Open***用户登录访问
  3. Spring IOC容器分析(1) -- BeanFactory
  4. 乔布斯和任正非相比,谁更厉害?
  5. 关于Decorator模式
  6. GitHub引入了Reactions来提供对issue和pull request的反馈
  7. CDKEY制作:为什么会有CDKEY产生机这样的破解工具?
  8. 组态王JAVA,组态王肿么实现点击按钮以后可以实现画面的运行
  9. Excel数据分析—散点图/气泡图
  10. SDM439/SDM429/SDM450 Sensors Overview (80-PF208-11)
  11. 天使投资人杨宁:百度轻应用让移动时代很多不可能成为可能
  12. 51单片机——LED1357亮,2468闪
  13. 高可靠环境 FileNet 系统介绍和应用实例
  14. css view a if属性,uni-app学习笔记(2)view属性控制css样式
  15. 【路科V0】systemVerilog基础3——字符串类型
  16. 一文聊透Netty核心引擎Reactor的运转架构
  17. android 各国时区
  18. Android App签名(证书)校验过程源码分析
  19. CentOS系统安装(7.8.2003)
  20. HAUT 1262 魔法宝石(spfa)(河南工业大学2017校赛)

热门文章

  1. 『程序员』 [程序人生]程序员几种不同的境界
  2. 用Canvas画圆环百分比进度条
  3. Jquery 获取日期date()对象,jquerydate
  4. iOS UIlabel文字排版(改变字间距行间距)分类
  5. 如何将存储在MongoDB数据库中的数据导出到Excel中?
  6. 微信开发中,H5的video标签使用
  7. 一些linux知识和http知识
  8. 逆向project实战--Acid burn
  9. 【开源程序(C++)】获取bing图片并自动设置为电脑桌面背景
  10. Zip4j开源jar包的简单使用