【问题标题】:Is there a way to output a sentence first then exit in tkinter有没有办法先输出一个句子然后在 tkinter 中退出
【发布时间】:2021-04-27 05:43:35
【问题描述】:

我想在用户按下错误按钮时输出一些句子,然后在他们回答错误并退出进程时使用 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()

【问题讨论】:

  • 只需创建一个新函数并在函数内调用任何你想要的东西。

标签: python tkinter game-development


【解决方案1】:

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

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

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

【讨论】:

  • 不建议这样做,最好只定义另一个调用它们的函数而不是使用 lambda
  • 这就像在开头使用def function1():def function2():并为它们添加不同的操作并使用command: function1(), function2()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多