有没有办法先输出一个句子然后在tkinter中退出

问题描述

我想在用户按下错误按钮时输出一些句子,然后在他们回答错误退出过程时使用 tkinter 模块退出(比如说“白痴”或其他什么)。但是,command=lambda:支持一个动作,当我添加多行时,python 解释器会输出错误。有没有办法对 tkinter 制作的窗口执行两个或多个操作?
这是代码的一部分,我希望代码输出 self.correspondingBehavior 部分,然后退出

class Q3(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self,text="MCQ test!!! Question 3: Organic chemistry is the study of the compounds that "
                                "make up living organisms. All organic molecules contain:",font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = tk.Button(self,text="Carbon only",command=lambda:self.correspondingBehavior('Wrong...check your '
                                                                   'notes and try '
                                                                   'again.'))
        button1.pack()

        button2 = tk.Button(self,text="Carbon and nitrogen",command=lambda:self.correspondingBehavior('Wrong...check your '
                                                                   'notes and try '
                                                                   'again.'))
        button2.pack()

        button3 = tk.Button(self,text="Carbon and hydrogen",command=lambda: controller.show_frame(Q4))
        button3.pack()

        button4 = tk.Button(self,text="Quit",command=lambda: controller.destroy())
        button4.pack()

解决方法

您可以使用 lambda 在 command 属性中给出多个命令(您可以调用多个函数)。为此,您可以使用列表。

例如,command=lambda:[root2.destroy(),next_step()]

这将首先销毁并退出 root2 窗口。然后也调用 next_step() 函数。