Tharushi Geethma
Tharushi Geethma

Reputation: 1289

bind two commands for one button

Im a complete beginner for programming.I want to combine two processes with a single button 'ok' in tkinter.

I want the program to execute according to the user input. If someone inputs as Arrival(vehicle), I want the program to be executed in one way and if someone inputs as 'departure(vehicle)', I want the program to be executed in another way.

How can i do this with a single ok button?

This is my way and it won't work!When i enter the vehicle number, arrival method and departure method both will be executed!

def OkClicked1(event=None):
    stepwindow.delete(0,END)

    vehicle=str(txtEntrXpression.get())

    Arrival(vehicle)

    Departure(vehicle)

Upvotes: 1

Views: 505

Answers (1)

AkaiShuichi
AkaiShuichi

Reputation: 154

If you want to have different functionality for the same button in different cases you should have an Entry or text-box in your window. The user will specify some details that will help the program distinguish between an arrival and a departure.

(I am using Python 3.3)

You can use it in the following way:

textbox1 = tkinter.Entry(root)
textbox1.pack()
OkButton = tkinter.Button(root, text = "Ok", command = lambda: OkClicked(textbox1)

The last line will ensure that the textbox is passed to the OnClicked function.

Inside the OnClicked function you can have this:

def OnClicked(textbox1, event=None):
    text = textbox1.get()

    stepwindow.delete(0,END)
    vehicle=str(txtEntrXpression.get())

    if text == "Arrival":
        Arrival(vehicle)
    elif text == "Departure":
        Departure(vehicle)

"Arrival" and "Departure" are just suggestions. Of course, you can have any string in place of them.

I hope this works for you.

Upvotes: 1

Related Questions