Python Forum
Get name of command button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get name of command button
#1
import tkinter as tk

root = tk.Tk()
myfont = "helvitica 25"
mybg = "light sky blue"
myheight = 4
mywidth = 60
myheight2 = 6
mywidth2 = 20
myheight3 = 3
mywidth3 = 20
labcol = "spring green"
anscol = "yellow"
otherbutcol = "light sky blue"
mypadx =0
mypady=0


class Main:

    def __init__(self, root):
        self.root = root
        root.geometry('1200x1200')
        self.First()

    def First(self):
        self.firstframe = tk.Frame(self.root)
        self.firstframe.grid(row=0, column=0)


        self.firstlabel = tk.Label(self.firstframe, text="firstlabel ", bg=labcol, font=myfont, relief="groove")
        self.firstlabel .config(height=myheight, width=mywidth)
        self.firstlabel .grid(row=1, column=1, sticky=tk.W)

        self.firstcommandbutton = tk.Button(self.firstframe, text="firstcommandbutton", bg=otherbutcol, font=myfont,
                                     relief="groove", command=self.Second)
        self.firstcommandbutton.config(height=myheight, width=mywidth)
        self.firstcommandbutton.grid(row=2, column=1, sticky=tk.W)
        self.quitbn = tk.Button(self.firstframe, text="Quit", font=myfont, bg="red", relief="groove",
                                command=self.firstframe.quit)
        self.quitbn.config(height=myheight, width=mywidth)
        self.quitbn.grid(row=3, column=1, sticky=tk.W)


    def Second (self):

        print("It worked")

m = Main(root)

root.mainloop()
import tkinter as tk

root = tk.Tk()
myfont = "helvitica 25"
mybg = "light sky blue"
myheight = 4
mywidth = 60
myheight2 = 6
mywidth2 = 20
myheight3 = 3
mywidth3 = 20
labcol = "spring green"
anscol = "yellow"
otherbutcol = "light sky blue"
mypadx =0
mypady=0


class Main:

    def __init__(self, root):
        self.root = root
        root.geometry('1200x1200')
        self.First()

    def First(self):
        self.firstframe = tk.Frame(self.root)
        self.firstframe.grid(row=0, column=0)


        self.firstlabel = tk.Label(self.firstframe, text="firstlabel ", bg=labcol, font=myfont, relief="groove")
        self.firstlabel .config(height=myheight, width=mywidth)
        self.firstlabel .grid(row=1, column=1, sticky=tk.W)

        self.firstcommandbutton = tk.Button(self.firstframe, text="firstcommandbutton", bg = otherbutcol, font=myfont,
                                     relief="groove", command=self.Second)
        self.firstcommandbutton.config(height=myheight, width=mywidth)
        self.firstcommandbutton.grid(row=2, column=1, sticky=tk.W)
        self.quitbn = tk.Button(self.firstframe, text="Quit", font=myfont, bg="red", relief="groove",
                                command=self.firstframe.quit)
        self.quitbn.config(height=myheight, width=mywidth)
        self.quitbn.grid(row=3, column=1, sticky=tk.W)


    def Second (self):

        print("It worked")

m = Main(root)

root.mainloop()
This code creates a command button called "firstcommandbutton". This button calls the method "Second". Can I create code in Second, that gives me the name of the firstcommandbutton, and reads it into a variable.? If that is not possible can I get the text of the firstcommandbutton?
Reply
#2
Why? If you want to access the button that called a finction it is easier to just pass the button as an argument.
self.firstcommandbutton.configure(command=lambda x = self.firstcommandbutton: self.Second(x))

def second(button):
    print(button['text']
ndc85430 likes this post
Reply
#3
This is how I changed the code based on your answer. It now gives me a
m = Main(root)
^
SyntaxError: invalid syntax


import tkinter as tk

root = tk.Tk)
myfont = "helvitica 25"
mybg = "light sky blue"
myheight = 4
mywidth = 60
myheight2 = 6
mywidth2 = 20
myheight3 = 3
mywidth3 = 20
labcol = "spring green"
anscol = "yellow"
otherbutcol = "light sky blue"
mypadx = 0
mypady = 0


class Main:

    def __init__(self, root):
        self.root = root
        root.geometry('1200x1200')
        self.First()

    def First(self):
        self.firstframe = tk.Frame(self.root)
        self.firstframe.grid(row=0, column=0)

        self.firstlabel = tk.Label(self.firstframe, text="firstlabel ", bg=labcol, font=myfont, relief="groove")
        self.firstlabel.config(height=myheight, width=mywidth)
        self.firstlabel.grid(row=1, column=1, sticky=tk.W)

        self.firstcommandbutton = tk.Button(self.firstframe, text="firstcommandbutton", bg=otherbutcol, font=myfont,
                                            relief="groove")
        self.firstcommandbutton.config(height=myheight, width=mywidth, command=lambda x = self.firstcommandbutton: self.Second(x))
        self.firstcommandbutton.grid(row=2, column=1, sticky=tk.W)
        self.quitbn = tk.Button(self.firstframe, text="Quit", font=myfont, bg="red", relief="groove",
                                command=self.firstframe.quit)
        self.quitbn.config(height=myheight, width=mywidth)
        self.quitbn.grid(row=3, column=1, sticky=tk.W)

    def second(button):
        print(button['text']

m = Main(root)

root.mainloop()
Reply
#4
You should fix the syntax error. The error is not this line "m = Main(root)".

Here are a couple of methods that receive a widget as an argument and perform some action on the widget.
import tkinter as tk
 
class MyWindow:
    def __init__(self, root):
        frame = tk.Frame(root)
        frame.pack(expand=True, fill=tk.BOTH)
        
        label = tk.Label(frame, text="Press the Button")
        label.pack(expand=True, fill=tk.BOTH, padx=5, pady=5)
 
        button = tk.Button(frame, text="This is the Button")
        button.config(command=lambda:self.button_pressed(button, label))
        button.pack(expand=True, fill=tk.BOTH, padx=5, pady=5)

        quitbn = tk.Button(frame, text="Quit", command=lambda:self.quit(root))
        quitbn.pack(expand=True, fill=tk.BOTH, padx=5, pady=5)
 
    def button_pressed(self, button, label):
        label['text'] = 'The button was pressed'
        button['state'] = tk.DISABLED
 
    def quit(self, window):
        print('Quitting')
        window.destroy()
 
root = tk.Tk()
root.geometry('200x200')
MyWindow(root)
root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class function does not create command button Heyjoe 2 477 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  [Tkinter] Command button, then more command buttons Heyjoe 4 856 Aug-08-2020, 11:28 AM
Last Post: Yoriz
  [Tkinter] button command tkinter Heyjoe 6 1,638 Jul-30-2020, 07:06 PM
Last Post: deanhystad
  Button Command Heyjoe 4 614 Jul-20-2020, 01:45 AM
Last Post: Heyjoe
  tkinter button not accessing the command when clicked jhf2 1 803 Nov-23-2019, 10:17 PM
Last Post: DT2000
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 1,795 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] command button automaticaly fired kferhat 2 887 Jun-16-2019, 08:21 PM
Last Post: kferhat
  [Tkinter] Button command getting TypeError: radsa() missing 3 required positional arguments nonzzo 2 1,244 Apr-22-2019, 12:36 PM
Last Post: Larz60+
  [Tkinter] how to assign a command to a button ChickenWithRice 2 2,525 Dec-08-2018, 09:35 PM
Last Post: ChickenWithRice
  [Tkinter] Tkinter newly created button does not execute command ChipsSlave 7 3,134 Jul-25-2018, 01:01 AM
Last Post: JUANCARLOS

Forum Jump:

User Panel Messages

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