подскажите, пожалуйста, почему функция self.tranform в первом сценарии вызывается без проблем (перемещает картинку вправо). А во втором, несколько усложненном, не работает:
1) первый
from tkinter import * class MMain(): def __init__(self, parent = None): canvas = Canvas(width=508, height=508, bg = '#aa9d93') canvas.pack(fill = BOTH, expand = YES) canvas.bind('<Right>', self.moveRight) canvas.focus_set() self.canvas = canvas self.photo = PhotoImage(file = 'numbers\\small_2.gif') self.canvas.create_image(10, 10, image = self.photo, anchor = NW, tag = 'asd') self.photo2 = PhotoImage(file = 'numbers\\small_2.gif') self.canvas.create_image(10, 150, image = self.photo, anchor = NW, tag = 'zxc') def moveRight(self, event): for i in [['asd', 248, 0, False], ['zxc', 248, 0, False]]: tagg = i[0] moveX = i[1] moveY = i[2] merge = i[3] print(tagg, moveX, moveY, merge) # работает self.tranform(tagg, moveX, moveY, merge) # работает def tranform(self, tagg, moveX, moveY, merge): if merge == False: if moveX > 0: move, moveX = 31, (moveX - 31) self.canvas.move(tagg, move, 0) if moveX: self.canvas.after(50, self.tranform, tagg, moveX, 0, merge) elif moveY > 0: move, moveY = 31, (moveY - 31) self.canvas.move(tagg, 0, move) if moveY: self.canvas.after(50, self.tranform, tagg, 0, moveY, merge) else: num = int(tagg[-4:]) if moveX > 0: move, moveX = 31, (moveX - 31) self.canvas.move(tagg, move, 0) if moveX: self.canvas.after(50, self.tranform, tagg, moveX, 0, merge) elif moveY > 0: move, moveY = 31, (moveY - 31) self.canvas.move(tagg, 0, move) if moveY: self.canvas.after(50, self.tranform, tagg, 0, moveY, merge) if __name__ == '__main__': MMain() mainloop()
2) второй
from tkinter import * class MMain(): photos = {} def __init__(self, parent = None): canvas = Canvas(width=508, height=508, bg = '#aa9d93') canvas.pack(fill = BOTH, expand = YES) canvas.bind('<Right>', self.moveRight) canvas.focus_set() self.canvas = canvas tagg = '260120002' self.photos[tagg] = PhotoImage(file = 'numbers\\small_2.gif') self.canvas.create_image(10, 10, image = self.photos[tagg], anchor = NW, tag = tagg) def moveRight(self, event): for tagg in sorted(self.photos, reverse = True): num = int(tagg[-4:]) if tagg[:3] == '384': continue elif tagg[:5] == '26012': L = [0, 0, 2] merge = False moveX = 0 moveY = 0 for i in L: if i > num: break elif i == 0: moveX += 124 continue elif i == num: moveX += 124 merge = True break if moveX > 0: print(tagg, moveX, moveY, merge) # работает self.tranform(tagg, moveX, moveY, merge) # НЕ РАБОТАЕТ! continue def tranform(self, tagg, moveX, moveY, merge): if merge == False: if moveX > 0: move, moveX = 31, (moveX - 31) self.canvas.move(tagg, move, 0) if moveX: self.canvas.after(50, self.tranform, tagg, moveX, 0, merge) elif moveY > 0: move, moveY = 31, (moveY - 31) self.canvas.move(tagg, 0, move) if moveY: self.canvas.after(50, self.tranform, tagg, 0, moveY, merge) else: num = int(tagg[-4:]) if moveX > 0: move, moveX = 31, (moveX - 31) self.canvas.move(tagg, move, 0) if moveX: self.canvas.after(50, self.tranform, tagg, moveX, 0, merge) elif moveY > 0: move, moveY = 31, (moveY - 31) self.canvas.move(tagg, 0, move) if moveY: self.canvas.after(50, self.tranform, tagg, 0, moveY, merge) if __name__ == '__main__': MMain() mainloop()
При этом функция print с теми же аргументами работает в обеих сценариях.
В прикреплении папка с обеими сценариями и картинками для тестирования