Home > database >  error when changing a Tkinter button command method
error when changing a Tkinter button command method

Time:04-17

Code:

from tkinter import * 

#screen 1
scr1 = Tk()
scr1.configure(bg='#2e3033')
canvas = []
teamCommentsButton = []

#update Vissuals
def updateTeams():
    for x in range(6):
        onClick = lambda : comments(x 10)
        canvas[x].itemconfig(teamCommentsButton[x], command = onClick)

def comments (team):
    print(team)
    comments = Toplevel(scr1)

for x in range(6):
    canvas.append(Canvas(scr1, width = 840, height = 326, bg='#2e3033', highlightthickness=1, highlightbackground='#2e3033'))
    teamCommentsButton.append(Button(canvas[x], text='☰', command = lambda : comments(x), width = 2, height = 1))
    teamCommentsButton[x].place(x = 20, y = 20)
    canvas[x].grid(column = 0 if x < 3 else 1, row = (x if x < 3 else x - 3))

scr1.title('Pit TV - Match')
updateTeams()
scr1.mainloop()

Error:

Traceback (most recent call last):
  File "c:\Users\user\Documents\Team 1710\Code\GKC2022\test.py", line 26, in <module>
    updateTeams()
  File "c:\Users\user\Documents\Team 1710\Code\GKC2022\test.py", line 13, in updateTeams
    canvas[x].itemconfig(teamCommentsButton[x], command = onClick)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2903, in itemconfigure
    return self._configure(('itemconfigure', tagOrId), cnf, kw)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1636, in _configure
    self.tk.call(_flatten((self._w, cmd))   self._options(cnf))
_tkinter.TclError: invalid boolean operator in tag search expression

I want to be able to change the parameter of a command in a Tkinter button but I get this error when I try to do it. I have tried changing the parameter to a constant onClick = lambda : comments(10) and I have tried directly putting the method call as the command command = comments(x 10) but both give me the same error

on top of that when I remove the call to updateTeams() the code runs without errors but prints 5 no matter which button I click. I would expect that it prints a range from 0-5 depending on the button I click because the parameter I set for each button is dependent on x.

here is what the window looks like when I remove updateTeams() window

CodePudding user response:

You have two problems in your Code:

Problem 1

The buttons are not items of the canvases.

You have to treat the button like a regular tkinter widget and use configure:

teamCommentsButton[x].configure(command=onClick)

If you want the button to actually be inside the canvas you have to add it to another frame and add that frame as an item to the window using:

canvas[x].create_window((20, 20), window=buttonFrame)

Problem 2

In Python lambda functions created in loops will execute the same function. That means your lambdas in updateTeams() will always use x = 15. This can be avoided by using an own function for creating lambdas:

def create_lambda(x):
    return lambda: comments(x   10)
  • Related