-1

Here is an example of what I am trying to convey:

from tkinter import *

def start():
    print("Start")
    B1.pack_forget()
    B2.pack()


def stop():
    B2.pack_forget()
    B1.pack()

root = Tk()
root.title("TestWin")

B1 = Button(root, text='start', command=start)
B1.pack()
B2 = Button(root, text='stop', command=stop)

root.mainloop()

Now I want to use if logic to get the name of the button currently packed. And it could look something like:

if <button_text_keyword> == 'start' then print('Start') elif <button_text_keyword> == 'stop' then print("Stop").

Can this be done??? Or Do I have to type a long code in order to achieve that???

Please suggest a good method to do what I want or rectify me.

Tom
  • 8,310
  • 2
  • 16
  • 36
prerakl123
  • 121
  • 1
  • 11

2 Answers2

0

If you want that on clicking the button your button text will get change then you try #this

from tkinter import *
root = Tk()
root.title("title")

def start():
    #for changing text on button
    button_name.config(text="new text on button")

B1 = Button(root, text='start', command=start)
B1.pack()
Community
  • 1
  • 1
  • No only changing the name is not what I wanted, that I did in the code that I provided in the question anyways thanks for trying to help but I got the correct answer – prerakl123 Jun 18 '20 at 16:42
0

Just to be clear, if the goal is just to print whatever text the button is showing, the simplest is to just have separate command functions, as you have done. It's then explicitly clear which button was there at the time of the click. So by this I mean, just add a print statement in the stop function.

But assuming there is a more nuanced situation where you actually need to determine what is packed in a different part of the program, this is an example based on what @acw1668 suggested in the comments:

from tkinter import *

packed = 'B1'

def start():
    global packed
    B1.pack_forget()
    B2.pack()
    packed = 'B2'

def stop():
    global packed
    B2.pack_forget()
    B1.pack()
    packed = 'B1'

def check():
    if packed == 'B1':
        print("Start")
    elif packed == 'B2':
        print("Stop")

root = Tk()
root.title("TestWin")

B1 = Button(root, text='start', command=start)
B1.pack(side=TOP)
B2 = Button(root, text='stop', command=stop)
B3 = Button(root, text='check', command=check)
B3.pack(side=BOTTOM)

root.mainloop()

The new button ("check") determines what button is packed, based on the flag packed (just to note, if you instead wrap the application in class (see here), you could avoid having to use global).

The above is more than sufficient, but if you actually want to check the button object itself, you can check the packed elements in a Frame with pack_slaves():

from tkinter import *

def start():
    B1.pack_forget()
    B2.pack()

def stop():
    B2.pack_forget()
    B1.pack()

def check():
    global button_frame
    button = button_frame.pack_slaves()[0]
    text = (button.cget('text'))
    if text == 'start':
        print('Start')
    elif text == 'stop':
        print('Stop')

root = Tk()
root.title("TestWin")

button_frame = Frame(root,)
button_frame.pack(side=TOP)

B1 = Button(button_frame, text='start', command=start)
B1.pack()
B2 = Button(button_frame, text='stop', command=stop)
B3 = Button(root, text='check', command=check)
B3.pack(side=BOTTOM)

root.mainloop()

This second example sounds more similar to the logic you described; i.e. find object corresponding to the pressed button, then get its text using cget, and route that to determine what is printed. Note that this example finds the button widget by using button = button_frame.pack_slaves()[0], so there is an assumption that there will only be clearly one button packed in button_frame.

Another option would be to use bind to program the button's effects, rather than the command parameter. You could then create a function like check in the above example to bind to both buttons, and you could use this technique to get the pressed widget (and then check it's identity/text, determine what to print, etc....).

Tom
  • 8,310
  • 2
  • 16
  • 36
  • Thanks @Tom both the methods worked great and I discovered correct usage of two more tkinter methods – prerakl123 Jun 18 '20 at 16:39
  • Thanks @Tom both the logics work great That if logic thing that I wrote was just the shortest possible example I could give otherwise I have different usage for it – prerakl123 Jun 18 '20 at 16:44