Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_drag_canvas.py
- from Tkinter import *
- root = Tk()
- def scroll_start(event):
- canvas.scan_mark(event.x, event.y)
- def scroll_move(event):
- canvas.scan_dragto(event.x, event.y, gain=1)
- canvas = Canvas(width=400, height=400, background="grey")
- xsb = Scrollbar(orient="horizontal", command=canvas.xview)
- ysb = Scrollbar(orient="vertical", command=canvas.yview)
- canvas.configure(yscrollcommand=ysb.set, xscrollcommand=xsb.set)
- canvas.configure(scrollregion=(0,0,1000,1000))
- xsb.grid(row=1, column=0, sticky="ew")
- ysb.grid(row=0, column=1, sticky="ns")
- canvas.grid(row=0, column=0, sticky="nsew")
- colors = "red", "orange", "yellow", "green", "blue"
- target = (0,0,1000,1000)
- sz = 100
- for n in colors:
- canvas.create_oval(target, outline=n, fill=n)
- target = target[0]+sz, target[1]+sz, target[2]-sz, target[3]-sz
- canvas.create_text(50,10, anchor="nw",
- text="CLICK AND DRAG TO MOVE THE CANVAS")
- # This is what enables scrolling with the mouse...
- canvas.bind("<ButtonPress-1>", scroll_start)
- canvas.bind("<B1-Motion>", scroll_move)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement