且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

tkinter按钮的多个命令

更新时间:2023-09-29 18:12:28

至少三个选项:

使用或​​ode>(如果有参数,则使用lambda):

using or (with lambda if arguments):

from tkinter import Tk, Button


root = Tk()

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

root.mainloop()

使用或​​ode>很重要,因为不起作用(根本不知道为什么或如何起作用)

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

或使用函数定义:

from tkinter import Tk, Button


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


root = Tk()

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

root.mainloop()

或带有lambda的列表:

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()