を(キャンバスの各要素を移動させるのではなく、キャンバスの表示領域をスクロールしません)。前者はマウスをクリックした場所を記憶し、後者はウィンドウを適切な量のピクセルでスクロールします。
注:gain
属性は、scan_moveto
に、マウスが動く各ピクセルごとに移動するピクセル数を示します。デフォルトでは10です。したがって、カーソルとキャンバスの間に1対1の相関関係が必要な場合は、この値を1に設定する必要があります(例のように)。ここで
は例です:
import Tkinter as tk
import random
class Example(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
self.canvas = tk.Canvas(self, width=400, height=400, background="bisque")
self.xsb = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)
self.ysb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.ysb.set, xscrollcommand=self.xsb.set)
self.canvas.configure(scrollregion=(0,0,1000,1000))
self.xsb.grid(row=1, column=0, sticky="ew")
self.ysb.grid(row=0, column=1, sticky="ns")
self.canvas.grid(row=0, column=0, sticky="nsew")
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
for n in range(50):
x0 = random.randint(0, 900)
y0 = random.randint(50, 900)
x1 = x0 + random.randint(50, 100)
y1 = y0 + random.randint(50,100)
color = ("red", "orange", "yellow", "green", "blue")[random.randint(0,4)]
self.canvas.create_rectangle(x0,y0,x1,y1, outline="black", fill=color)
self.canvas.create_text(50,10, anchor="nw",
text="Click and drag to move the canvas")
# This is what enables scrolling with the mouse:
self.canvas.bind("<ButtonPress-1>", self.scroll_start)
self.canvas.bind("<B1-Motion>", self.scroll_move)
def scroll_start(self, event):
self.canvas.scan_mark(event.x, event.y)
def scroll_move(self, event):
self.canvas.scan_dragto(event.x, event.y, gain=1)
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
はどうもありがとう、それは私がまさに必要です! 無限(またはほぼ無限)のキャンバスを持つことができますので、右/左/上/下にスクロールできます。 – Basj
@Basj:実際の制限は文書化されていませんが、無限ではないとは思いません。スクロール領域を大きくして実験するだけです。私の推測は、座標は32ビットのintです。 –
このスクロールコード@BryanOakleyに感謝します。 「ズーム」の場合、 'canvas.scale'のようなものを使用しますか?私は、ズームするときに最初から自分でやって実装しましたが、おそらく 'tkinter'に何か組み込みがあると思いますか? – Basj