Tkinter:如何使用箭头键滚动整个 canvas?

Tkinter: How to scroll an entire canvas using arrow keys?

我正在编写一个 python 应用程序,它使用 Tkinter,它 canvas 用于从调色板绘制图表。图表可能会变得很大,我目前正在使用鼠标按钮 (ButtonPress-1) 按住并拖动整个 canvas.

我很难理解如何仅通过使用箭头键(键盘向上、向下、向左和向右)实现整个 canvas 的滚动。

求助!

canvas的xviewyview方法用于滚动canvas。这些与您用来将滚动条连接到 canvas 的方法完全相同。您可以滚动 "units" 或 "pages"。 "units" 由 canvas 选项 xscrollincrementyscrollincrement 定义。

您要绑定的事件是 <Up><Down><Left><Right>

将它们放在一起,您将创建如下所示的绑定:

self.canvas.bind("<Left>",  lambda event: self.canvas.xview_scroll(-1, "units"))
self.canvas.bind("<Right>", lambda event: self.canvas.xview_scroll( 1, "units"))
self.canvas.bind("<Up>",    lambda event: self.canvas.yview_scroll(-1, "units"))
self.canvas.bind("<Down>",  lambda event: self.canvas.yview_scroll( 1, "units"))

您需要确保 canvas 有焦点。您可以明确地为其提供焦点,但您可能还希望通过单击鼠标来将焦点也提供给 canvas:

self.canvas.focus_set()
self.canvas.bind("<1>", lambda event: self.canvas.focus_set())

这是一个完整的工作示例:

import Tkinter as tk
import random

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.canvas = tk.Canvas(self, background="bisque")
        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
        self.hsb = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)
        self.canvas.configure(xscrollcommand=self.hsb.set, yscrollcommand=self.vsb.set)

        self.canvas.grid(row=0, column=0, sticky="nsew")
        self.vsb.grid(row=0, column=1, sticky="ns")
        self.hsb.grid(row=1, column=0, sticky="ew")

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        for i in range(100):
            x = random.randint(0, 1000)
            y = random.randint(0, 1000)
            width = random.randint(10, 50)
            height = random.randint(10, 50)
            fill = random.choice(("red", "orange", "yellow", "green", "blue", "violet"))
            self.canvas.create_rectangle(x, y, x+width, y+height, fill=fill)

        self.canvas.configure(scrollregion = self.canvas.bbox("all"))

        self.canvas.bind("<1>",     lambda event: self.canvas.focus_set())
        self.canvas.bind("<Left>",  lambda event: self.canvas.xview_scroll(-1, "units"))
        self.canvas.bind("<Right>", lambda event: self.canvas.xview_scroll( 1, "units"))
        self.canvas.bind("<Up>",    lambda event: self.canvas.yview_scroll(-1, "units"))
        self.canvas.bind("<Down>",  lambda event: self.canvas.yview_scroll( 1, "units"))

        self.canvas.focus_set()

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()