Apr-18-2020, 01:46 PM
Pass attributes into the __init__, attributes used in methods of the class assign as class attributes
import tkinter as tk import raspberryGPIO functionsPage = tk.Tk() functionsPage.geometry('1024x600') class Toggle(tk.Frame): def __init__(self, button_label, which_button, master=None, **kwargs): tk.Frame.__init__(self, master, **kwargs) self.which_button = which_button self.btn = tk.Button(self, text=button_label, width=20, height=5, bg="yellow", command=self.clicked) self.btn.grid(column=0, row=0, pady=10) def clicked(self): if self.btn['bg'] == "yellow": self.btn.configure(bg="red") GPIO.output(self.which_button, GPIO.LOW) else: self.btn.configure(bg="yellow") GPIO.output(self.which_button, GPIO.HIGH) which_button1 = 'Asign this' which_button2 = 'Asign this' btn1 = Toggle('button_label1', which_button1, functionsPage) btn1.grid() btn2 = Toggle('button_label2', which_button2, functionsPage) btn2.grid() functionsPage.mainloop()