how to toggle different buttons with same function on CustomTkinter

54 Views Asked by At

I'm trying to create a grid of buttons that toogle or at least change once pressed. But I couldn't manage to diferenciate each button to change their parameters through self.button.config() Or even develop a toggle function that handle all buttons

I cut down some parts for better reading of my code, its build over one of the samples in customtkinter's documentation

import customtkinter


customtkinter.set_appearance_mode("System")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("dark-blue")  # Themes: "blue" (standard), "green", "dark-blue"


class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        self.dezenas_frame = customtkinter.CTkFrame(self, width=30, corner_radius=0)
        self.dezenas_frame.grid(row=0, column=0, rowspan=4, sticky="nsew")
        self.dezenas_frame.grid_rowconfigure(4, weight=1)
        self.logo_label = customtkinter.CTkLabel(self.dezenas_frame, text="text text!",
                                                 font=customtkinter.CTkFont(size=10, weight="bold"))
        self.logo_label.grid(row=0, column=0, padx=15, pady=(10, 10))

        c_r = 1
        c_c = 1
        for cnt in range(1, 61):
            self.button = customtkinter.CTkButton(self.dezenas_frame, text=cnt, width=40,
                                                command=self.toggleState)
            self.button.grid(row=c_r, column=c_c, padx=1, pady=1)

            if c_c == 10: c_c = 0; c_r += 1
            c_c += 1

    def toggleState(self):
        self.config(self, state='something')


if __name__ == "__main__":
    app = App()
    app.mainloop()

Also tried differentiate each button (button_01, button_02, ... button_60) with tk but still got stuck

 self.button_01 = tk.Button(self.right_frame, text="01", relief="raised", command=self.togg)
 self.button_01.grid(row=0, column=0, padx=5, pady=5)
 self.button_02 = tk.Button(self.right_frame, text="02", relief="raised", command=self.togg)
 self.button_02.grid(row=0, column=1, padx=5, pady=5)
.
.
.
self.button_60 = tk.Button(self.right_frame, text="60", relief="raised", command=self.togg)
self.button_60.grid(row=5, column=9, padx=5, pady=5)
1

There are 1 best solutions below

0
elavec On

Thanks to jasonharper's comment I managed to find a solution


import customtkinter as ctk


ctk.set_appearance_mode("System")  # Modes: "System" (standard), "Dark", "Light"
ctk.set_default_color_theme("dark-blue")  # Themes: "blue" (standard), "green", "dark-blue"


class App(ctk.CTk):
    def __init__(self):
        super().__init__()

        self.dezenas_frame = ctk.CTkFrame(self, width=30, corner_radius=0)
        self.dezenas_frame.grid(row=0, column=0, rowspan=4, sticky="nsew")
        self.dezenas_frame.grid_rowconfigure(4, weight=1)
        self.logo_label = ctk.CTkLabel(self.dezenas_frame, text="text text!",
                                                 font=ctk.CTkFont(size=10, weight="bold"))
        self.logo_label.grid(row=0, column=0, padx=15, pady=(10, 10))

        self.buttons_dezenas = []
        c_r = 1
        c_c = 1
        for cnt in range(60):
            button_text = cnt +1
            self.buttons_dezenas.append(ctk.CTkButton(self.dezenas_frame, text=f"{button_text:02d}", width=40, fg_color=('#3B8ED0', '#1F6AA5'),
                                                                command= lambda b=cnt : self.toggleState(self.buttons_dezenas[b])))
            self.buttons_dezenas[cnt].grid(row=c_r, column=c_c, padx=1, pady=1)

            if c_c == 10: c_c = 0; c_r += 1
            c_c += 1

    def toggleState(self, b_num):
        # parameter-changing-code 


if __name__ == "__main__":
    app = App()
    app.mainloop()