Reputation: 4246
I succesed implement canvas and scrollBar horizontal and vertical type. I add every time when i click on canvas new object. If i scroll canvas in any right and cant add any new object or app adds new object on wrong position (no scroll position).
I need delta from hor and ver scrollBar to get correct input params.
canvasFrame = tkinter.Frame(window,
width=screen_width - 110,
height=screen_height - 110,
background="#f3ffff")
canvasFrame.place(x=105, y=20, width=screen_width - 115, height=screen_height - 130)
canvas = tkinter.Canvas(
canvasFrame,
width=screen_width * initValues.canvasScreenCoeficientW,
height=screen_height * initValues.canvasScreenCoeficientH,
background=initValues.windowBackgroundColor,
scrollregion=(0,0,
screen_width * initValues.canvasScreenCoeficientW,
screen_height * initValues.canvasScreenCoeficientH)
)
# canvas.place(x=0, y=0, width= 2 *screen_width - 120, height=screen_height - 130)
canvas.bind("<Button-1>", collectMouseEventData)
# Scroll bars for canvas
hCanvasBar = tkinter.Scrollbar(canvasFrame,orient=tkinter.HORIZONTAL)
hCanvasBar.pack(side=tkinter.BOTTOM,fill=tkinter.X)
hCanvasBar.config(command=canvas.xview)
vCanvasBar = tkinter.Scrollbar(canvasFrame,orient=tkinter.VERTICAL)
vCanvasBar.pack(side=tkinter.RIGHT,fill=tkinter.Y)
vCanvasBar.config(command=canvas.yview)
canvas.config(xscrollcommand=hCanvasBar.set, yscrollcommand=vCanvasBar.set)
canvas.pack(side=tkinter.LEFT,expand=True,fill=tkinter.BOTH)
Method recalculateX
and recalculateY
just sticks click point to the nearlest grid line.
I need something like : += deltax
def collectMouseEventData(event):
if event.y > 0 and event.x > 50:
# print("clicked at", event.x, event.y)
x = event.x
y = event.y
local = "x:" + str(event.x) + ", y:" + str(event.y)
appCoordinate.configure(text=local)
if initValues.stickler["enabledX"] == True:
x = editorStickler.recalculateX(x)
# print("Enabled X stickler.")
if initValues.stickler["enabledY"] == True:
y = editorStickler.recalculateY(y)
# print(" Y ")
localModel = 0
#RESOURCE_INDENTITY
filename = selectedTex.split('\\')
filenameStr = "require('../../imgs/" + filename[len(filename) -2] + "/" + filename[len(filename) -1] + "')"
if insertBox.get() == "ground":
localModel = StaticGrounds(x,
y,
initValues.ELEMENT_WIDTH,
initValues.ELEMENT_HEIGHT,
filenameStr,
initValues.tilesX,
initValues.tilesY)
Upvotes: 0
Views: 881
Reputation: 385870
The canvas has methods for converting coordinates from window coordinates to canvas coordinates. You should pass event.x
and event.y
to the canvasx
and canvasy
methods, respectively.
Here's a simple example:
import tkinter as tk
def handle_click(event):
canvas = event.widget
x = canvas.canvasx(event.x)
y = canvas.canvasy(event.y)
canvas.create_rectangle(x-2, y-2,x+1,y+2, fill="red")
canvas.create_text(x,y+8, text=f"({x},{y})", anchor="n")
canvas.configure(scrollregion=canvas.bbox("all"))
root = tk.Tk()
canvas = tk.Canvas(root, background='bisque')
ysb = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
xsb = tk.Scrollbar(root, orient="horizontal", command=canvas.xview)
canvas.configure(yscrollcommand=ysb.set, xscrollcommand=xsb.set)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
canvas.grid(row=0, column=0, sticky="nsew")
ysb.grid(row=0, column=1, sticky="ns")
xsb.grid(row=1, column=0, sticky="ew")
canvas.bind("<1>", handle_click)
root.mainloop()
Upvotes: 4