simons21 simons21 - 1 year ago 47
Python Question

Tkinter button that calls two variables when pressed?

I have a function that takes in a start time and end time of an activity. I would like a message box to pop up to tell them if the start time begins after the end time (for example starting at 18.00 but finishing at 15.00). I decided that a message box would be perfect for telling them they are wrong and pushing them back to the beginning of the function.

How would I be able to make the button that when pressed, calls two commands. One that goes back to the beginning of the function and another that destroys the message box?

def addtimeslot():
hour1 = variable_st.get()
starttimehour = str(datetime.time(hour1,minute,second))
hour2 = variable_end.get()
endtimehour = str(datetime.time(hour2,minute,second))
if starttimehour >= endtimehour:
win = Toplevel()
win.title('invalid input')
message = "You have selected invalid input. Please try again"
Label(win, text = message).pack()
button = Button(win, text = 'ok', command = addtimeslot AND win.destroy).pack

continue code

I know the 'AND' shouldn't be there its just showing what I would like.

btn_add_final = ttk.Button(window1, text = "Add timeslot",width = 15, command = addtimeslot) =300,y=275)

updated to show button that calls function.

Answer Source

Make a function that calls both of your functions:

def fun1_and_fun2():

# Function holding your button
# Stuff
# ....
button = Button(win, text = 'ok', command = fun1_and_fun2).pack
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download