首页 > 解决方案 > tkinter:多个列表框不会触发向上/向下箭头键

问题描述

我想通过将事件绑定到箭头键来从 tkinter 的多列表框中选择项目。

多列表框在这里工作正常!用鼠标滚动。但它不适用于向上/向下箭头键。我在代码中添加了向上/向下箭头键绑定,但是当我点击箭头键时它没有触发,而且这些解决方案都不是来自link1!和链接2!曾为我工作。

以下是我在我提供的链接中添加到代码中的内容:

...
lb.bind("<Down>", lambda e, s=self: s._OnEntryDown(e.y))
lb.bind("<Up>", lambda e, s=self: s._OnEntryUp(e.y))
...

...
def downarrow(self, *args):
   for l in self.lists:
       if self.selection < l.size()-1:
           l.select_clear(self.selection)
           self.selection += 1
           l.select_set(self.selection)


def uparrow(self, *args):
    for l in self.lists:
        if self.selection > 0:
            self.l.select_clear(self.selection)
            self.selection -= 1
            self.l.select_set(self.selection)

我希望通过向上/向下箭头键进行选择可以像在 tkinter 中使用单个列表框一样完美地工作。请我需要帮助!

标签: pythonpython-3.xtkinter

解决方案


在进行了几次网上冲浪后,我能够看到这个链接!在 github 上,但这是用 python 2.x 编写的,所以我将它修改为也可以与 python 3.x 一起使用。下面是修改后的代码:

#!/usr/bin/env python
import copy
import csv
import logging
import os
import platform
import tkinter as tk
from tkinter import *
from tkinter import ttk
import tkinter.constants as Tkc
import tkinter.filedialog as Tkfc
import tkinter.font as Tkf
import tkinter.messagebox as Tkmb
import webbrowser
import sys

logging.getLogger().setLevel(logging.DEBUG)


class MultiListbox(ttk.Frame):

    def __init__(self, parent, lists):
        ttk.Frame.__init__(self, parent)
        self.lists = []
        self.colmapping = {}
        self.orig_data = None
        self.fonts = {}
        self.y_sel = None
        for label, width in lists:
            frame = Frame(self)
            frame.pack(side=Tkc.LEFT, expand=Tkc.YES, fill=Tkc.BOTH)

            sort_button = Button(frame, text=label, borderwidth=1, relief=Tkc.RAISED)
            sort_button.pack(fill=Tkc.X)
            sort_button.bind("<Button-1>", self._sort)
            sort_button.config(font=self.header_font)

            self.colmapping[sort_button] = (len(self.lists), 1)
            list_box = Listbox(frame, width=width, borderwidth=0, selectborderwidth=0,
                              relief=Tkc.FLAT, exportselection=Tkc.FALSE)
            list_box.pack(expand=Tkc.YES, fill=Tkc.BOTH)
        self.lists.append(list_box)
            list_box.bind("<B1-Motion>", lambda e, s=self: s._select(e.y))
            list_box.bind("<Button-1>", lambda e, s=self: s._select(e.y))
            list_box.bind("<Leave>", lambda e: "break")
            list_box.bind("<B2-Motion>", lambda e, s=self: s._b2motion(e.x, e.y))
            list_box.bind("<Button-2>", lambda e, s=self: s._button2(e.x, e.y))
            list_box.bind("<Double-Button-1>", lambda e, s=self: s._activate(e.y))

        frame = Frame(self)
        frame.pack(side=Tkc.LEFT, fill=Tkc.Y)
        frame_label = Label(frame, borderwidth=1, relief=Tkc.RAISED)
        frame_label.pack(fill=Tkc.X)
        scroll_bar = Scrollbar(frame, orient=Tkc.VERTICAL, command=self._scroll)
        scroll_bar.pack(expand=Tkc.YES, fill=Tkc.Y)
        self.lists[0]["yscrollcommand"] = scroll_bar.set

        # Configure scrolling by arrow keys and Page Up/Down.
        self.bind_all("<Up>", lambda e, s=self: s._scroll("scroll", "-1", "units", select=True))
        self.bind_all("<Down>", lambda e, s=self: s._scroll("scroll", "1", "units", select=True))
        self.bind_all("<Next>", lambda e, s=self: s._scroll("scroll", "1", "pages", select=True))
        self.bind_all("<Prior>", lambda e, s=self: s._scroll("scroll", "-1", "pages", select=True))
        self.bind_all("<Return>", lambda e, s=self: s._activate(e.y))

       self.master.protocol("WM_DELETE_WINDOW", self.master.destroy)

    @property
    def header_font(self):
        if "header" in self.fonts:
            return self.fonts["header"]
        font_families = sorted(Tkf.families(self.master))
        if "Liberation Sans" in font_families:
            family = "Liberation Sans"
        else:
            family = "Tahoma"
        font = Tkf.Font(family=family, size=13, weight=Tkf.BOLD)
        self.fonts["header"] = font
        return font

    def _sort(self, event):

        # Get the listbox to sort by (mapped by the header buttons)
        originating_button = event.widget
        column, direction = self.colmapping[originating_button]

        # Make an in-memory copy of all the table data.
        table_data = self.get(0, Tkc.END)
        if self.orig_data is None:
            self.orig_data = copy.deepcopy(table_data)
        row_count = len(table_data)

        # Remove any old sort indicators (if they exist)
        for button in self.colmapping:
            button_text = button.cget("text")
            if button_text[0] == "[":
                button.config(text=button_text[4:])

        # Sort data based on direction.
        button_label = originating_button.cget("text")
        if direction == 0:
            table_data = self.orig_data
        elif direction == 1:
            originating_button.config(text="[+] {0}".format(button_label))
            table_data.sort(key=lambda obj: obj[column], reverse=False)
        else:  # direction == -1
            originating_button.config(text="[-] {0}".format(button_label))
            table_data.sort(key=lambda obj: obj[column], reverse=True)

        # Clear and refill the widget.
        self.delete(0, Tkc.END)
        for row in range(row_count):
            self.insert(Tkc.END, table_data[row])

        # Finally, toggle the direction flag.
        if direction == 1:
            direction = -1
        else:
            direction += 1
        self.colmapping[originating_button] = column, direction

    def _activate(self, y):
        item_info = self.get(self.curselection()[0])
        logging.info("Opening PyPI web page for item: %s", item_info)
        pypi_url = "https://pypi.python.org/pypi/{0[0]}".format(item_info)
        webbrowser.open_new(pypi_url)

    def _select(self, y):
        row = self.lists[0].nearest(y)
        logging.info("Selecting Y point %s (got row %s)", y, row)
        return self._select_row(row)

    def _select_row(self, row):
        logging.info("Selecting row %d", row)
        self.selection_clear(0, Tkc.END)
        self.selection_set(row)
        # self.see(row)
        return "break"

    def _button2(self, x, y):
        logging.info("Button 2 at (%d, %d)", x, y)
        for list_widget in self.lists:
            list_widget.scan_mark(x, y)
        return "break"

    def _b2motion(self, x, y):
        logging.info("B2 Motion to (%d, %d)", x, y)
        for list_widget in self.lists:
            list_widget.scan_dragto(x, y)
        return "break"

    def _scroll(self, *args, select=False):
        #select = kwargs.pop("select", False)
        logging.info("Scrolling -- args: %s, select: %s", args, select)

        if select and self.curselection():
            new_index, should_do_scroll = self.get_new_selection(args)
            if new_index is None:
                logging.debug("No selection change for args: %s - scrolling...", args)
                should_do_scroll = True
            else:
                old_index = int(self.curselection()[0])
                logging.debug("Changing selection from index %d to %d", old_index, new_index)
                self._select_row(new_index)
        else:
            should_do_scroll = True

        if should_do_scroll:
            for list_widget in self.lists:
                list_widget.yview(*args)

    def get_new_selection(self, scroll_args):
        """
    If selection change upon scrolling is enabled, return the new index that should be selected after the
    scroll operation finishes. If the new index is currently visible, just select it and skip the actual
    scrolling process entirely.

    :param list scroll_args: The arguments passed to the scrollbar widget
    :return tuple: The index that should be selected afterward, followed by its current "selectability"
    """
        cur_selection = self.curselection()

        # If the scrollbar is being dragged, or if nothing is currently selected, then do not select anything.
        if scroll_args[0] != "scroll" or not cur_selection:
            return None, False
        amount = int(scroll_args[1])
        pixel_dict = self.get_pixel_dict()
        page_size = len(pixel_dict) - 2 if scroll_args[2] == "pages" else 1
        scroll_diff = amount * page_size
        old_index = int(cur_selection[0])
        new_index = max(0, min(self.lists[0].size() - 1, old_index + scroll_diff))
        return new_index, new_index not in pixel_dict

    def curselection(self):
        return self.lists[0].curselection()

    def delete(self, first, last=None):
        for list_widget in self.lists:
            list_widget.delete(first, last)

    def get(self, first, last=None):
        result = []
        for list_widget in self.lists:
            result.append(list_widget.get(first, last))
        if last:
            return map(*[None] + result)
        return result

    def index(self, index):
        self.lists[0].index(index)

    def insert(self, index, *elements):
        for elem in elements:
            i = 0
            for list_widget in self.lists:
                list_widget.insert(index, elem[i])
                i += 1

    def size(self):
        return self.lists[0].size()

    def see(self, index):
        for list_widget in self.lists:
            list_widget.see(index)

    def selection_anchor(self, index):
        for list_widget in self.lists:
            list_widget.selection_anchor(index)

    def selection_clear(self, first, last=None):
        for list_widget in self.lists:
            list_widget.selection_clear(first, last)

    def selection_includes(self, index):
        return self.lists[0].selection_includes(index)

    def selection_set(self, first, last=None):
        for list_widget in self.lists:
           list_widget.selection_set(first, last)

    def get_pixel_dict(self):
        list_box = self.lists[0]
        height = list_box.winfo_height() + 1
        pixel_dict = {list_box.nearest(height): height}
        for pixel in range(height, 0, -1):
            pixel_dict[list_box.nearest(pixel)] = pixel
        max_index, bottom_y = max(pixel_dict.items())
        item_height = bottom_y - pixel_dict.get(max_index - 1, 1)
        while bottom_y + item_height < height:
            max_index += 1
            bottom_y += item_height
            pixel_dict[max_index] = bottom_y
        pixel_dict.pop(max_index)
        return pixel_dict


if __name__ == '__main__':
    tk = Tk()
    Label(tk, text='MultiListbox').pack(side=TOP)
    mlb = MultiListbox(tk, (('Subject', 40), ('Sender', 20), ('Date', 10)))

    for i in range(5000):
        mlb.insert(END, ('Important Message: %d' % i, 'John Doe %d' % i, '10/10/%04d' % (1900+i)))
        mlb.pack(expand=YES,fill=BOTH,side=TOP)
    mlb.selection_set(0)
    tk.mainloop()

推荐阅读