暂停/停止命令 python 代码 运行

Command to halt / stop python code running

我设计了两个图形用户界面。当 python 代码为 运行 时打开一个 GUI,当用户退出第一个 GUI 时打开第二个 GUI。

第一个 GUI 用于通知用户第二个 GUI 存在问题,并包含解决问题的解决方案。我在完成第二个 GUI 后添加了第一个 GUI,并意识到无法修复的设计问题。

现在,第一个 GUI 上有一个 'Understand and Accept' 按钮。我想添加一个停止 python 代码 运行ning.

的按钮

我已经创建了一个非常基本的版本来表达我在下面的意思。我删除了不必要的位并重命名了其他位以便更好地解释,所以这就是网格值不匹配的原因。

import tkinter as tk
from tkinter import *

def runwarningwindow():
    #This is the first GUI which opens warning tab
    warningscreen = Tk()
    warningscreen.resizable(width=False, height=False)
    warningscreen.title("First Window - Trial Warning Window")

    #This is the Warning message
    warningmessage = Label(warningscreen, text="Warning Message")
    warningmessage.grid(row=1, column=0)
    #This is the Accept Button
    warningunderstoodbutton = tk.Button(warningscreen, text='Understand and Accept', command=warningscreen.destroy)
    warningunderstoodbutton.grid(row=3, column=0)
    rejectedButton= tk.Button(warningscreen, text='Stop')
    rejectedButton.grid(row=4, column=0)
    warningscreen.mainloop()

runwarningwindow()

trialGUI = Tk()
trialGUI.geometry('710x320')
trialGUI.title("Second GUI - Test GUI")
trialGUI.mainloop()

我知道一种可能的解决方案是将 'trialGUI' 变成

def runtrailGUI():
    trialGUI = Tk()
    trialGUI.geometry('710x320')
    trialGUI.title("Second GUI - Test GUI")
    trialGUI.mainloop()

然后将 'Understand and Accept' 按钮设为 运行,但这是我试图避免的解决方案。第二个 GUI 的代码超过 3000 行(并且还在增加,我还在增加),所以我正在寻找另一种解决方案。是否有停止 python 代码为 运行 的命令?

当你想停止程序时,可以使用以下命令:

raise SystemExit