tkinter 按钮的多个命令

multiple commands for a tkinter button

我一直在尝试在 1 个按钮中执行 2 个命令。我读过使用 lambda 可以解决问题。但是我的情况有点不同。按下按钮时,一个命令是销毁现有的 GUI,第二个命令是打开另一个 SERVER GUI。下面是我现有的按钮功能。

exit_button = Button(topFrame, text='Quit', command=window.destroy)

如何使用相同的按钮打开另一个 GUI?

感谢您的帮助。

编辑:-

我现在已经创建了以下函数:

def close_func():
os.kill(os.getpid(), signal.SIGINT)
GUI_Interface()
window.destroy()
server_socket.close()

GUI_Interface 是我需要在关闭现有 .py 文件后调用的函数。如果我把 GUI_Interface 作为 close_func() 的第一个命令,那么它真的不会回到第二步并且永远不会关闭 existing.py 文件。

如果我把GUI_Interface放在最后,它只会关闭现有的,nevr会打开新.py文件的功能

至少三个选项:

使用 or(如果参数为 lambda):

from tkinter import Tk, Button


root = Tk()

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

root.mainloop()

使用 or 很重要,因为 and 不起作用(完全不知道为什么或如何起作用)

或使用函数定义:

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 的列表:

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