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



我设计了两个gui。一个GUI在python代码运行时打开,第二个GUI在用户退出第一个GUI时打开。

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

现在,第一个GUI上有一个"理解和接受"按钮。我想添加一个按钮来停止python代码的运行。

我在下面创建了一个非常基本的版本。我删去了不必要的部分,并重新命名了其他部分,以便更好地解释,所以这就是网格值不匹配的原因。

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

,然后让"理解并接受"按钮运行它,但这是我试图避免的解决方案。第二个GUI的代码超过3000行(而且还在增加),所以我正在寻找另一个解决方案。是否有一个命令来停止python代码正在运行?

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

raise SystemExit

最新更新