Skip to content Skip to sidebar Skip to footer

Multiple Commands For A Tkinter Button

I have been trying to execute 2 commands in 1 button. I have read that using lambda can solve the problem. But my situation is a little different. On button press, one command is t

Solution 1:

Three options at least:

using or (with lambda if arguments):

from tkinter import Tk, Button


root = Tk()

Button(root, text='Press', command=lambda: print('hello') or root.destroy() orprint('hi')).pack()

root.mainloop()

important to use or because and didn't work (don't know why or how this works at all)

or use function definitions:

from tkinter import Tk, Button


deffunc():
    print('hello')
    root.destroy()
    print('hi')


root = Tk()

Button(root, text='Press', command=func).pack()

root.mainloop()

or lists with lambda:

from tkinter import Tk, Button


def func():
    print('hello')
    root.destroy()
    print('hi')


root = Tk()

Button(root, text='Press', command=lambda: [print('hello'), root.destroy()]).pack()

root.mainloop()

Post a Comment for "Multiple Commands For A Tkinter Button"