0

I have two classes, the main window (App class) and the frame class (Test). Inside the Test class i added a button (test_btn) which calls a method inside the class called "print_t" which simply prints "Test". On the main window class I add the Test class frame, and also create a variable called "minus_btn" that accesses the test_btn and bind it. The bind calls a method inside the main window called "print_e". So when I press the button the console will output "Test" and then the event of the bind.

import customtkinter

class Test(customtkinter.CTkFrame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.test_btn = customtkinter.CTkButton(master=self, text='-', command=self.print_t)
        self.test_btn.grid(row=0, column=0, padx=30, pady=30)

    def print_t(self):
        print("Test")

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

        self.test = Test(master=self)
        self.test.grid(row=0, column=0)

        minus_btn = self.test.test_btn
        minus_btn.bind("<Button-1>", self.print_e)

    def print_e(self, event):
        print(event)

if __name__ == '__main__':
    # RUN
    app = App()
    app.mainloop()

But if I replace customtkinter to normal tkinter:

import tkinter as tk

class Test(tk.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.test_btn = tk.Button(master=self, text='-', command=self.print_t)
        self.test_btn.grid(row=0, column=0, padx=30, pady=30)

    def print_t(self):
        print("Test")

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.test = Test(master=self)
        self.test.grid(row=0, column=0)

        minus_btn = self.test.test_btn
        minus_btn.bind("<Button-1>", self.print_e)

    def print_e(self, event):
        print(event)

if __name__ == '__main__':
    # RUN
    app = App()
    app.mainloop()

The app outputs first the event and then the "Test" print. The only thing i changed was the library but by doing that it changes the order. I need the bind run first and then the command but don't understand if im doing something wrong. Is there a different way to do this?

Marcelo_M
  • 13
  • 3

1 Answers1

0

What prevents you from doing everything through bind?

import customtkinter

class Test(customtkinter.CTkFrame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.test_btn = customtkinter.CTkButton(master=self, text='-')
        self.test_btn.grid(row=0, column=0, padx=30, pady=30)

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

        self.test = Test(master=self)
        self.test.grid(row=0, column=0)

        minus_btn = self.test.test_btn
        minus_btn.bind("<Button-1>", self.print_e)

    def print_e(self, event):
        print(event)
        print("Test")

if __name__ == '__main__':
    # RUN
    app = App()
    app.mainloop()

------------------------------
<ButtonPress event state=Mod1 num=1 x=62 y=11>
Test
<ButtonPress event state=Mod1 num=1 x=62 y=11>
Test
Сергей Кох
  • 1,417
  • 12
  • 6
  • 13