【发布时间】:2020-09-18 04:09:04
【问题描述】:
我可以在Double Click 上实现这一点。
这是我的sample code。
单击时的示例图像
双击示例图片
我的代码的一些解释:
- 我创建了两个列表框:
list_box_one,list_box_two - 同时滚动两个小部件。
- 我想通过单击从两个列表框中选择项目。
import tkinter as tk
class App:
def __init__(self):
self.root = tk.Tk()
self.scroll = tk.Scrollbar(orient="vertical", command=self.Onscroll)
self.scroll.pack(side="right", fill="y")
self.list_box_one = tk.Listbox(self.root,
borderwidth=0,
yscrollcommand=self.scroll.set)
self.list_box_one.pack(side="left", fill="both", expand=True)
self.list_box_one.config(exportselection=False,
highlightthickness=0,
selectbackground='red')
self.list_box_one.bind("<MouseWheel>", self.OnMouseWheel)
self.list_box_one.bind('<Double-Button-1>', self.fileSelection)
self.list_box_two = tk.Listbox(self.root,
borderwidth=0,
yscrollcommand=self.scroll.set)
self.list_box_two.pack(side="left", fill="both", expand=True)
self.list_box_two.config(exportselection=False,
highlightthickness=0,
selectbackground='red')
self.list_box_two.bind("<MouseWheel>", self.OnMouseWheel)
self.list_box_two.bind('<Double-Button-1>', self.fileSelection)
for i in range(100):
self.list_box_one.insert("end", "item %s" % i)
self.list_box_two.insert("end", "item %s" % i)
self.root.mainloop()
def Onscroll(self, *args):
self.list_box_one.yview(*args)
self.list_box_two.yview(*args)
def OnMouseWheel(self, event):
self.list_box_one.yview_scroll(-1 * int(event.delta / 120), "units")
self.list_box_two.yview_scroll(-1 * int(event.delta / 120), "units")
return "break"
def fileSelection(self, event):
widget = event.widget
index = widget.curselection()[0] # Index Of Selected Item
self.list_box_two.selection_clear(0, 'end') # Clear Old Selected Item
self.list_box_one.selection_clear(0, 'end') # Clear Old Selected Item
self.list_box_one.selection_set(index) # Selecting Current Item
self.list_box_two.selection_set(index) # Selecting Current Item
app = App()
【问题讨论】:
标签: python python-3.x tkinter