[Python] Tkinter's Canvas - bad screen distance ""

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    I'm working on a paint app using Tkinter's Canvas
    and recently I've been trying to get the undo/redo system in place.
    but there's a crucial problem with the redo function.
    it doesn't have some values to recreate the elements even though I've stored them in lists.

    Here is a simplified example:

    class Window(tk.Tk):
    def __init__(self):
    super().__init__()
    ...

    def paint(self, event):
    if self.previous_point != [0, 0]
    self.line = self.canvas.create_line(..........)
    self.line_group.append(self.line)
    if event.type == tk.EventType.ButtonRelease:
    self.line_groups.append(self.line_group)
    self.previous_point = [self.x, self.y]
    self.line = ''
    if event.type == tk.EventType.ButtonRelease:
    self.line_group = []
    if event.type == '5':
    self.previous_point = [0, 0]

    def undo(self, event=None, custom_index=-1):
    items = self.canvas.find_all()
    if items:
    last_group, cords = [], []
    if self.points_mode.get() == 'drag':
    if self.line_groups:
    line_group = self.line_groups[-1]
    loop_range = len(line_group) + 1
    for line in range(1, loop_range):
    ind = line * -1
    last_group.append(items[ind])
    cords.append((self.canvas.bbox(items[ind])))
    self.canvas.delete(items[ind])

    self.last_items.append((last_group, self.dash_groups[-1], cords))
    print(self.last_items[-1])

    del self.dash_groups[-1]
    del self.line_groups[-1]
    else:
    try:
    self.canvas.delete(items[custom_index])
    last_group.append(items[custom_index])
    except IndexError:
    pass
    def redo(self):
    if self.last_items:
    last_line, last_dash, cords = self.last_items[-1]
    item_type = self.canvas.type(last_line[0])
    f_color, o_color = self.canvas.itemcget(last_line[0], 'fill'), self.canvas.itemcget(last_line[0], 'outline')
    width = self.canvas.itemcget(last_line[0], 'width')
    self.iterate_lines(last_line, (f_color, o_color), width, last_dash, cords)
    del self.last_items[-1]

    def iterate_lines(self, group, colors, width, dl_size, cords):
    self.canvas.update_idletasks()
    for index ,l in enumerate(group):
    print(l)
    '''+ always returning none on the lines!
    * doesnt work even when items are not deleted and just hidden'''
    cords = list(cords.values())
    x, y, x1, y1 = cords[index]
    print(f'index {index}')
    fill, outline = colors
    self.line = self.canvas.create_line(x, y, x1, y1, dash=dl_size,
    fill=fill, width=width,
    smooth=self.smooth_line.get(), outline=outline)
    self.line_group.append(self.line)
    self.line_groups.append(self.line_group)
    self.all_items.append(self.line_group)



    return self.tk.getint(self.tk.call(
    ~~~~~~~~~~~~^
    self._w, 'create', itemType,
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    *(args + self._options(cnf, kw))))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    _tkinter.TclError: bad screen distance ""


    This is very weird and can be the slightest error, but I've checked many probable "big" causes and didn't have luck

    Aftermath: I've even tried to capture the values in their initial creation, even printed them and it all was going well, but I can't create that line with them

    Continue reading...

Compartilhe esta Página