1

I have a button in my window that will create another button when it is pressed. I keep a list of the buttons that are added. The creation of the button uses the same method and thus each button uses the same command:

def addMacro(self):
    # find the CAN pane
    for i, s in enumerate(self.objectPanes):
        if str(s).find('.can') != -1:
            canPane = self.objectPanes[i]

    # Create the message list
    messages = []
    func = messageData[self.messageName.get()][2]
    if func != "Invalid message number":
        addresses = self.getAddresses()
        # Form the message.  If too long, it will return multiple packets
        messages = func(canPane.ch0[0], int(messageData[self.messageName.get()][1]), addresses, self.var,
                        self.funcVars, 'macro')
        print('Macro Messages = ', messages)
        for msg in messages:
            messageComplete, link, message, parsed, mName, parsedIdentifier, messageTime, identifier = \
                manageFilterLinks(msg, self.messageList, self.bits)
        #i = len(self.macroButtons) - 1

    # Create the new button
    self.macroButtons.append([tk.Button(master=self.macroBtnFrame, text=self.messageName.get(),
                                        command=self.macroClick), identifier, message])

    # Bind right click to the button
    i = len(self.macroButtons) - 1
    self.macroButtons[i][0].bind('<Button-3>', self.macroRightClick)

    # Place the button(s) in a grid
    x = 0
    y = 0
    for macro in self.macroButtons:
        macro[0].grid(row=x, column=y, padx=(5, 0), pady=(5, 0))
        y += 1
        if y == 5:
            y = 0
            x += 1

The method called when any of the created buttons is pressed is macroClick:

def macroClick(self):
    print('Macro Click')
    print('Self = ', self)
    print('Macro Children = ', self.macroBtnFrame.children)
    for btn in self.macroButtons:
        print(btn)
        if btn[0] == self:
            print('Send message: ', btn[1], btn[2])

The code in this method so far is only for testing. I want to be able to access the button list, self.macroButtons, to get the added parameters (identifier and message) that were added with this line of code in method addMacro():

    self.macroButtons.append([tk.Button(master=self.macroBtnFrame, text=self.messageName.get(),
                                        command=self.macroClick), identifier, message])

The problem I have is that I don't know which button in the grid has been clicked on so I don't know which item in the list to draw from.

Any ideas?

Thanks,

Rick

Rickl
  • 13
  • 2
  • Try appending the button alone, not `[button]`. – Delrius Euphoria Feb 25 '21 at 20:14
  • Changing the button command to a lamda as follows: ' # Create the new button i = len(self.macroButtons) self.macroButtons.append([tk.Button(master=self.macroBtnFrame, text=self.messageName.get(), command=lambda i=i: self.macroClick(i)), identifier, message])' – Rickl Feb 25 '21 at 21:16

0 Answers0