Moving Around A Canvas Without Using Scrollbars
Background:
I have a program using Tkinter as the basis of the GUI. The program has a
canvas which is populated with a large number of objects. Currently, in
order to move all objects on the screen I am simply binding a movement
function to the tag 'all' which of course moves all objects on the screen.
However, it is vital for me to keep track of all canvas object positions-
i.e. after every move I log the new position, which seems unnecessarily
complicated.
Question:
What is the best way to effectively scroll/drag around the whole canvas
(several times the size of the screen) using only the mouse (not using
scrollbars)?
My Attempts:
Apologies if this is a trivial question, I have implemented scrollbars and
found several guides to setting up scrollbars, but none that deal with
this particular requirement.
Thank you for any help!
Example of disused scrollbar method:
from Tkinter import *
class Canvas_On:
def __init__(self, master):
self.master=master
self.master.title( "Example")
self.c=Canvas(self.master, width=find_res_width-20,
height=find_res_height, bg='black', scrollregion=(0,0,5000,5000))
horizontalbar=Scrollbar(self.master,orient=HORIZONTAL)
horizontalbar.pack(side=BOTTOM,fill=X)
horizontalbar.config(command=self.c.xview)
verticalbar=Scrollbar(self.master,orient=VERTICAL)
verticalbar.pack(side=RIGHT,fill=Y)
verticalbar.config(command=self.c.yview)
self.c.config(xscrollcommand=horizontalbar.set,
yscrollcommand=verticalbar.set)
self.c.pack(side=LEFT,expand=True,fill=BOTH)
draw=Drawing_Utility(self.c)
draw.drawer(self.c)
class Drawing_Utility:
def __init__(self, canvas):
self.canvas=canvas
self.canvas.focus_set()
def drawer(self, canvas):
self.canvas.create_oval(50,50,150,150, fill='orange')
self.canvas.create_oval(1000,1000,1100,1100, fill='orange')
root=Tk()
find_res_width=root.winfo_screenwidth()
find_res_height=root.winfo_screenheight()
run_it=Canvas_On(root)
root.mainloop()
No comments:
Post a Comment