Issue with differentiating between two of the same

2019-04-12 02:45发布

问题:

The program I created allows for any letter on the keyboard the user types to be written on the turtle graphics canvas. In my program, I have also created a Python menu to which a Point object (for each letter function) is written to every time a user executes a function/draws a letter. However, because of the nature of my program, the user can also attach two of the same functions to the menu. If two of the same things get attached to the menu, for example two functions, I need a way to differentiate between them somehow. To do this, I have created a counter in another function and called that counter function in the function where the menu is written to, like so:

Counter function:

def increase():
    if not hasattr(increase, "counter"):
        increase.counter = 0
    increase.counter += 1

Code block when menu is written to:

global loki
kli.config(state = NORMAL)
loki = ("{}".format(po.getfunction()))
increase() #<-- Counter function
undo1.add_command(label = str(increase.counter) + Point.__str__(po), command = lambda: selectundo(undo1.index(po)))

Point.__str__ is this method in the Point class:

def __str__(self):
    return "({})".format(self.function)

However, I keep getting this error whenever I select something from the menu:

undo1.add_command(label = str(increase.counter) + Point.__str__(po), command = lambda: selectundo(undo1.index(po)))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 2782, in index
i = self.tk.call(self._w, 'index', index)
tkinter.TclError: bad menu entry index "(<function draw_O at 0x105834d90>)"

I am thinking it has something to do with the following function, which undoes the function that is selected from the menu, but I am not sure:

def selectundo(x):
    # This function undoes the function selected from the menu
    for ty in range(x, len(function)):
        undoHandler()
        update()
        listen()

Although, before I concatenated str(increase.counter) to Point.__str__(po), it worked perfectly.

So, what am I doing wrong here? Any help at all is much appreciated! :)

EDIT: Just to clear up what I am trying to do and why, I am trying to differentiate between two (or more) of the same functions if they are written to the menu and I am doing this because of the selectundo function (shown above) since, for example, if the user draws two (or more) of the same letter, I need to be able to differentiate between them, because right now, when I can't, the selectundo function undoes ALL instances of that letter, NOT just the first instance of what is pressed on the menu, which is what I actually want the program to do. If what I am trying to do to accomplish the task is not possible or if there is a better way to accomplish the task, please tell be about any/the other way that I can use to accomplish the task. I hope this helps alleviate any confusion! :)