【问题标题】:How to select item from two different Listbox on Single click如何在单击时从两个不同的列表框中选择项目
【发布时间】:2020-09-18 04:09:04
【问题描述】:

我可以在Double Click 上实现这一点。

这是我的sample code

单击时的示例图像

双击示例图片

我的代码的一些解释:

  1. 我创建了两个列表框:list_box_onelist_box_two
  2. 同时滚动两个小部件。
  3. 我想通过单击从两个列表框中选择项目。
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


    【解决方案1】:

    您可以在两个bind(...) 中将&lt;Double-Button-1&gt; 替换为&lt;&lt;ListboxSelect&gt;&gt;

    还要在fileSelection()函数末尾添加以下两行:

    self.list_box_one.see(index)
    self.list_box_two.see(index)
    

    【讨论】:

    • 你好@acw1668先生,你能解决我最近的问题吗。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    相关资源
    最近更新 更多