Home>

There are buttons A to E, and what I want to achieve is to write the code briefly below.

Press the A button to insert A into self.master.txt_dumy and
I want to insert B into self.master.txt_dumy when I press the B button.

I tried to create command = self.btn_click in self.master.btn_1 using the btn_click function.
With this method, you have to create another function for B to E that does the same processing.

If possible, I would like to realize it with one function, but is there a good way?

  def btn_click (self):
        abc = self.master.btn_1 ['text']
        self.master.txt_dumy.insert (tk.END, abc)
      self.master.btn_1 = tkinter.Button (self.master, text ='A', font = font2, bg = "# faefe6", height = 2, width = 10, command = btn_click)
        self.master.btn_1.place (x = 550, y = 50)
        self.master.btn_2 = tkinter.Button (self.master, text ='B', font = font2, bg = "# faefe6", height = 2, width = 10)
        self.master.btn_2.place (x = 550, y = 100)
        self.master.btn_3 = tkinter.Button (self.master, text ='C', font = font2, bg = "# faefe6", height = 2, width = 10)
        self.master.btn_3.place (x = 550, y = 150)
        self.master.btn_4 = tkinter.Button (self.master, text ='D', font = font2, bg = "# faefe6", height = 2, width = 10)
        self.master.btn_4.place (x = 450, y = 100)
        self.master.btn_5 = tkinter.Button (self.master, text ='E', font = font2, bg = "# faefe6", height = 2, width = 10)
        self.master.btn_5.place (x = 650, y = 100)
  • Answer # 1

    If you register an event handler with bind,
    Of the argumentevent.widgetYou can browse your own widget with

    import tkinter as tk
    def btn_click (event):
        button = event.widget
        print (button ["text"])
    if __name__ == "__main__":
        root = tk.Tk ()
        button = tk.Button (root, text = "A")
        button.bind ("<ButtonRelease-1>", btn_click)
        button.pack ()
        root.mainloop ()

    Another way: bind arguments with functools.partial

    When registering with command, there is no argument, but if you use functools.partial
    Unlike bind, you can bind any number of arguments.

    However, since an object that associates the function and the argument is created for each button,
    For brevity, it's better to use bind (if possible).

    from functools import partial
    import tkinter as tk
    def btn_click (button):
        print (button ["text"])
    if __name__ == "__main__":
        root = tk.Tk ()
        button = tk.Button (root, text = "A")
        button ["command"] = partial (btn_click, button)
        button.pack ()
        root.mainloop ()

    NG: CANNOT ... No instance was created at the time of registering command in the constructor

    button = tk.Button (root, text = "A", command = functools.partial (btn_click, button))

    Other method 2: Use subclasses

    General-purpose solution using classes

    import tkinter as tk
    class MyButton (tk.Button):
        def __init __ (self, * args, ** kwargs):
            super () .__ init __ (* args, ** kwargs)
        def onclick (self): #Access via instance
            print (self ["text"])
    if __name__ == "__main__":
        root = tk.Tk ()
        button = MyButton (root, text = "A")
        button ["command"] = button.onclick
        button.pack ()
        root.mainloop ()