Python Forum
[Tkinter] Creation of Buttons with Shared Command Inside Class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Creation of Buttons with Shared Command Inside Class
#1
I am trying to refactor some code into classes and methods that I have for a LED matrix GUI. When I create the matrix of buttons I created a lambda to recall the number of the button when it was created, but when I click a button it gives an error. This line of code worked when it was not inside a class but now that its transferring the variable "i" to a method within the class it just says the argument is missing when I click a button. please ignore errors within the click method. I cant really move on while this is not working so the click method is not finished being adjusted to the class.

Error:
File "C:\python_gui\LEDMATRIX.py", line 40, in <lambda> self.b = tk.Button(self.window, text="*", command=lambda i=self.but_num: ButtonMatrix.click(i)) TypeError: click() missing 1 required positional argument: 'i'


def makematrix(self):
        self.but_num=0
        #create matrix of buttons
        for row in range(1, self.num_leds[0]+1):
            for column in range(self.num_leds[1]):
                self.b = tk.Button(self.window, text="*", command=lambda i=self.but_num: ButtonMatrix.click(i))
                if (row % 2) == 0:
                    self.b.grid(row=row, column=33-column, padx=self.pad, pady=self.pad, columnspan=2)
                else:
                    if row == 1:
                        self.b.grid(row=row, column=column, padx=self.pad, pady=self.pad, columnspan=1)
                    else:
                        self.b.grid(row=row, column=column, padx=self.pad, pady=self.pad, columnspan=3)
                self.but_num+=1
                self.button.append(self.b)    
def click(self, i):
    if self.button[i].cget('bg') == self.values[LEDcolorslide.get()]:
        self.button[i].config(bg=self.values[bgLEDcolorslide.get()])
    else:
        self.button[i].config(bg=self.values[LEDcolorslide.get()])
Reply
#2
Don't call the class click method ButtonMatrix.click(i) call self's click method self.click(i)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class function does not create command button Heyjoe 2 480 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  Creating a frame with 4 command buttons Heyjoe 5 549 Aug-21-2020, 03:16 PM
Last Post: deanhystad
  [Tkinter] Command button, then more command buttons Heyjoe 4 858 Aug-08-2020, 11:28 AM
Last Post: Yoriz
  [PyQt] I get a name Name Error: when calling a method inside a class radoo 2 536 Jun-11-2020, 05:02 PM
Last Post: radoo
  [Tkinter] How to add an argument to a buttons command call petterg 2 1,505 Jun-20-2019, 09:05 PM
Last Post: petterg

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020