鱼C论坛

 找回密码
 立即注册
查看: 4911|回复: 7

[已解决]TKinter中多个Listbox怎么实现鼠标联动

[复制链接]
发表于 2017-4-16 02:11:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
如图所示,我创建了三个Listbox,当鼠标在其中一个Listbox区域悬停并滚动鼠标的时候只能使它本身的列表上下滚动,怎么实现当在任意Listbox中滚动鼠标,其他Listbox也跟着滚动,嗯,功能有些奇怪,不知道该怎么实现,求大神帮助哈哈哈哈哈哈
最佳答案
2017-4-17 04:51:57
  1. >>> import tkinter as tk
  2. >>> def fn():
  3.         root=tk.Tk()
  4.         ls1=tk.Listbox(root)
  5.         ls2=tk.Listbox(root)
  6.         ls1.bind('<MouseWheel>',mw)
  7.         ls1.pack()
  8.         ls2.pack()
  9.         for i in range(20):
  10.                 ls1.insert(i,i)
  11.                 ls2.insert(i,i)
  12.         return root,ls1,ls2

  13. >>> def mw(e):
  14.         ls2.yview_scroll(-4*(e.delta//120),'units')

  15.        
  16. >>> root,ls1,ls2=fn()
复制代码
2017-04-16_020755.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-16 08:37:06 | 显示全部楼层
请帖全代码~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-16 10:44:57 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-16 16:36:36 | 显示全部楼层
这个是现在的代码,大神们看看该如何处理,实现L21、L22、L23、L24、L25这五个Listbox的鼠标联动~~

  1. # -*- coding: utf-8 -*-
  2. from tkinter import *
  3. import os

  4. #设置工作路径
  5. path = "D:\\Sch_Pcb_Lib\\Pcb_Lib"
  6. os.chdir(path)

  7. items = os.listdir(path)#获取路径中所有文件列表

  8. dra_list = []#提取dra后缀文件
  9. for names in items:
  10.   if names.endswith(".dra"):
  11.     dra_list.append(names)
  12. len_dra_list = len(dra_list)#取列表长度

  13. psm_list = []#提取psm后缀文件
  14. for names in items:
  15.   if names.endswith(".psm"):
  16.     psm_list.append(names)
  17. len_psm_list = len(psm_list)#取列表长度   

  18. txt_list = []#提取txt后缀文件
  19. for names in items:
  20.   if names.endswith(".txt"):
  21.     txt_list.append(names)
  22. len_txt_list = len(txt_list)#取列表长度
  23.    
  24. log_list = []#提取log后缀文件
  25. for names in items:
  26.   if names.endswith(".log"):
  27.     log_list.append(names)
  28. len_log_list = len(log_list)#取列表长度  

  29. del_list = []#提取log后缀文件
  30. for names in items:
  31.   if names.endswith(",1"):
  32.     del_list.append(names)
  33.   if names.endswith(",2"):
  34.     del_list.append(names)
  35.   if names.endswith(".jrl"):
  36.     del_list.append(names)
  37. len_del_list = len(del_list)#取列表长度

  38. def delList1(event):#双击事件
  39.     index = L21.curselection()#返回当前选中项的索引
  40.     os.remove(L21.get(index))#获取索引以及值
  41.     L21.delete(L21.curselection())

  42. def delList2(event):#双击事件
  43.     index = L22.curselection()#返回当前选中项的索引
  44.     os.remove(L22.get(index))#获取索引以及值
  45.     L22.delete(L22.curselection())

  46. def delList3(event):#双击事件
  47.     index = L23.curselection()#返回当前选中项的索引
  48.     os.remove(L23.get(index))#获取索引以及值
  49.     L23.delete(L23.curselection())

  50. def delList4(event):#双击事件
  51.     index = L24.curselection()#返回当前选中项的索引
  52.     os.remove(L24.get(index))#获取索引以及值
  53.     L24.delete(L24.curselection())

  54. def delList5(event):#双击事件
  55.     index = L25.curselection()#返回当前选中项的索引
  56.     os.remove(L25.get(index))#获取索引以及值
  57.     L25.delete(L25.curselection())


  58. root = Tk()
  59. root.title('封装管理')
  60. root.attributes('-alpha',0.8)
  61. root.resizable(width=False, height=False)

  62. L11  = Label(root, bg = '#F0F0F0', fg = "red", width = 20,font = ('微软雅黑', '10'), text = 'Dra',anchor="w", )
  63. L11.grid(row = 0, column = 0, padx = 1, pady = 1)

  64. L12  = Label(root, bg = '#F0F0F0', fg = "red", width = 20,font = ('微软雅黑', '10'), text = 'Psm',anchor="w", )
  65. L12.grid(row = 0, column = 1, padx = 1, pady = 1)

  66. L13  = Label(root, bg = '#F0F0F0', fg = "red", width = 20,font = ('微软雅黑', '10'), text = 'Txt',anchor="w", )
  67. L13.grid(row = 0, column = 2, padx = 1, pady = 1)

  68. L14  = Label(root, bg = '#F0F0F0', fg = "red", width = 20,font = ('微软雅黑', '10'), text = 'Log',anchor="w", )
  69. L14.grid(row = 0, column = 3, padx = 1, pady = 1)

  70. L15  = Label(root, bg = '#F0F0F0', fg = "red", width = 20,font = ('微软雅黑', '10'), text = 'Del',anchor="w", )
  71. L15.grid(row = 0, column = 4, padx = 1, pady = 1)

  72. L21  = Listbox(root, bg = '#F0F0F0',\
  73.         width = 20,\
  74.         font = ('微软雅黑', '10'),
  75.         borderwidth = 0,\
  76. )
  77. for item in dra_list:                 # 第一个小部件插入数据
  78.     L21.insert(0,item)
  79. L21.grid(row = 1, column = 0, padx = 1, pady = 1)
  80. #L21.bind('<Double-Button-1>', delList1)

  81. L22  = Listbox(root, bg = '#F0F0F0',\
  82.         width = 20,\
  83.         font = ('微软雅黑', '10'),
  84.         borderwidth = 0,\
  85. )
  86. for item in psm_list:                 # 第二个小部件插入数据
  87.     L22.insert(0,item)
  88. L22.grid(row = 1, column = 1, padx = 1, pady = 1)
  89. #L22.bind('<Double-Button-1>', delList2)

  90. L23  = Listbox(root, bg = '#F0F0F0',\
  91.         width = 20,\
  92.         font = ('微软雅黑', '10'),
  93.         borderwidth = 0,\
  94. )
  95. for item in txt_list:                 # 第三个小部件插入数据
  96.     L23.insert(0,item)
  97. L23.grid(row = 1, column = 2, padx = 1, pady = 1)
  98. #L23.bind('<Double-Button-1>', delList3)

  99. L24  = Listbox(root, bg = '#F0F0F0',\
  100.         width = 20,\
  101.         font = ('微软雅黑', '10'),
  102.         borderwidth = 0,\
  103. )
  104. for item in log_list:                 # 第四个小部件插入数据
  105.     L24.insert(0,item)
  106. L24.grid(row = 1, column = 3, padx = 1, pady = 1)
  107. #L24.bind('<Double-Button-1>', delList4)

  108. L25  = Listbox(root, bg = '#F0F0F0',\
  109.         width = 20,\
  110.         font = ('微软雅黑', '10'),
  111.         borderwidth = 0,\
  112. )
  113. for item in del_list:                 # 第五个小部件插入数据
  114.     L25.insert(0,item)
  115. L25.grid(row = 1, column = 4, padx = 1, pady = 1)
  116. L25.bind('<Double-Button-1>', delList5)

  117. v1 = StringVar()
  118. v1.set(len_dra_list)
  119. L31  = Label(root, bg = '#F0F0F0', fg = "red", width = 20,font = ('微软雅黑', '10'), anchor="w", textvariable = v1)
  120. L31.grid(row = 2, column = 0, padx = 1, pady = 1)

  121. v2 = StringVar()
  122. v2.set(len_psm_list)
  123. L32  = Label(root, bg = '#F0F0F0', fg = "red", width = 20,font = ('微软雅黑', '10'), anchor="w", textvariable = v2)
  124. L32.grid(row = 2, column = 1, padx = 1, pady = 1)

  125. v3 = StringVar()
  126. v3.set(len_txt_list)
  127. L33  = Label(root, bg = '#F0F0F0', fg = "red", width = 20,font = ('微软雅黑', '10'), anchor="w", textvariable = v3)
  128. L33.grid(row = 2, column = 2, padx = 1, pady = 1)

  129. v4 = StringVar()
  130. v4.set(len_log_list)
  131. L34  = Label(root, bg = '#F0F0F0', fg = "red", width = 20,font = ('微软雅黑', '10'), anchor="w", textvariable = v4)
  132. L34.grid(row = 2, column = 3, padx = 1, pady = 1)

  133. v5 = StringVar()
  134. v5.set(len_del_list)
  135. L35  = Label(root, bg = '#F0F0F0', fg = "red", width = 20,font = ('微软雅黑', '10'), anchor="w", textvariable = v5)
  136. L35.grid(row = 2, column = 4, padx = 1, pady = 1)


  137. mainloop()




复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-17 04:51:57 | 显示全部楼层    本楼为最佳答案   
  1. >>> import tkinter as tk
  2. >>> def fn():
  3.         root=tk.Tk()
  4.         ls1=tk.Listbox(root)
  5.         ls2=tk.Listbox(root)
  6.         ls1.bind('<MouseWheel>',mw)
  7.         ls1.pack()
  8.         ls2.pack()
  9.         for i in range(20):
  10.                 ls1.insert(i,i)
  11.                 ls2.insert(i,i)
  12.         return root,ls1,ls2

  13. >>> def mw(e):
  14.         ls2.yview_scroll(-4*(e.delta//120),'units')

  15.        
  16. >>> root,ls1,ls2=fn()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-4-17 10:38:47 | 显示全部楼层

谢谢大神,确实很好用,涨知识了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-18 09:07:10 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-18 09:07:39 | 显示全部楼层

ttk教程写好了没
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-16 02:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表