A
A
Alexey2020-01-04 08:09:38
Python
Alexey, 2020-01-04 08:09:38

Why do object coordinates not match when scrolling on canvas, python?

Good time everyone!
Made scrolling of objects on canvas through the canvas.move() method - changing the coordinates of an object when scrolling with the mouse wheel (canvas1.bind_all("", fun_mooving)). Everything turned out well, all objects move as they should. But the problem surfaced when I added several objects of the Entry type to the canvas. If you smoothly turn the mouse wheel, then a slight delay is noticeable when moving Entry objects, while other objects (texts, pictures) move without problems, as they should. If the mouse wheel is turned sharply, then the movement of Entry can go astray - not get up to the coordinates set by it. What could be the problem?
5e101d2dade33659799678.png
Program code below:

import tkinter as tk
from tkinter import*

global v_entry

root = tk.Tk()
root.geometry('600x800+10+10')
root.resizable(width=False, height=False)

def xxx():
    global v_entry, list_v
    txt1 = 'txt1txt1txt1txt1txt1txt1txt1'
    v_01 = canvas2.create_text(30, 10, text=txt1, anchor=NW)
    v_entry[0] =Entry(canvas2, bd = 0, justify = CENTER, bg = '#7F7F7F', width = 6)
    (v_entry[0]).insert(0, '000')
    (v_entry[0]).place(x=100, y=25)
    
    txt1 = 'txt2txt2txt2txt2txt2txt2txt2'
    v_02 = canvas2.create_text(30, 50, text=txt1, anchor=NW)
    v_entry[1] =Entry(canvas2, bd = 0, justify = CENTER, bg = '#7F7F7F', width = 6)
    (v_entry[1]).insert(0, '101')
    (v_entry[1]).place(x=100, y=75)
    
    txt1 = 'txt3txt3txt3txt3txt3txt3txt3'
    v_03 = canvas2.create_text(30, 100, text=txt1, anchor=NW)
    v_entry[2] =Entry(canvas2, bd = 0, justify = CENTER, bg = '#7F7F7F', width = 6)
    (v_entry[2]).insert(0, '202')
    (v_entry[2]).place(x=100, y=125)
    
    txt1 = 'txt4txt4txt4txt4txt4txt4txt4'
    v_04 = canvas2.create_text(30, 150, text=txt1, anchor=NW)
    v_entry[3] =Entry(canvas2, bd = 0, justify = CENTER, bg = '#7F7F7F', width = 6)
    (v_entry[3]).insert(0, '303')
    (v_entry[3]).place(x=100, y=175)
    
    txt1 = 'txt5txt5txt5txt5txt5txt5txt5'
    v_05 = canvas2.create_text(30, 200, text=txt1, anchor=NW)
    v_entry[4] =Entry(canvas2, bd = 0, justify = CENTER, bg = '#7F7F7F', width = 6)
    (v_entry[4]).insert(0, '404')
    (v_entry[4]).place(x=100, y=225)
    
    txt1 = 'tx6txt6txt6txt6txt6txt6txt6'
    v_06 = canvas2.create_text(30, 250, text=txt1, anchor=NW)
    v_entry[5] =Entry(canvas2, bd = 0, justify = CENTER, bg = '#7F7F7F', width = 6)
    (v_entry[5]).insert(0, '505')
    (v_entry[5]).place(x=100, y=275)
    
    txt1 = 'tx7txt7tx7txt7txt7txt7txt7'
    v_07 = canvas2.create_text(30, 300, text=txt1, anchor=NW)
    v_entry[6] =Entry(canvas2, bd = 0, justify = CENTER, bg = '#7F7F7F', width = 6)
    (v_entry[6]).insert(0, '606')
    (v_entry[6]).place(x=100, y=325)
    
    txt1 = 'tx8txt8txt8txt8txt8txt8txt8'
    v_08 = canvas2.create_text(30, 350, text=txt1, anchor=NW)
    v_entry[7] =Entry(canvas2, bd = 0, justify = CENTER, bg = '#7F7F7F', width = 6)
    (v_entry[7]).insert(0, '707')
    (v_entry[7]).place(x=100, y=375)
    
    list_v = [v_01, v_02, v_03, v_04, v_05, v_06, v_07, v_08]

def fun_mooving(event):              #прокрутка объектов на canvas2
    global v_entry, list_v
    
    y=int(event.delta/2)
    
    for i1 in list_v:
        canvas2.move(i1, 0, y)
        
        
    for i in v_entry:        
        y1 = i.winfo_y()
        y1 += y        
        i.place(x = 100, y = y1)
        

canvas1 = Canvas(root, width=600, height=800, highlightthickness=0)    #родительский канвас (общий)
canvas1.place(x=0, y=0)#  упаковка общего канваса для всех окон

canvas2 = Canvas(canvas1, width=600, height=750, highlightthickness=0)
canvas2.place(x=0, y=50)

v_entry = []
for i in range (8): 
    v_entry.append(Entry(canvas2, bd = 0, justify = CENTER, width = 6))

xxx()
canvas2.bind_all("<MouseWheel>", fun_mooving)
root.mainloop()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2020-01-04
@berckut172

entry does not move through the canvas.move method, I tried it - it gives an error:
canvas2.move(v_entry[i], 0, y):
i2 = 0
for i1 in list_v:
canvas2.move(i1, 0, y)
canvas2.move( v_entry[i2], 0, y)
i2+=1
(error: invalid logical operator in tag search expression)
maybe I don't understand something?)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question