【发布时间】:2021-05-02 10:51:51
【问题描述】:
我设计了两个 GUI。运行 python 代码时打开一个 GUI,当用户退出第一个 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 代码的命令?
【问题讨论】:
-
由于 tkinter 中的所有内容都在主线程上运行,调度一些工作有助于减少 GUI 线程挂起:oreilly.com/library/view/python-cookbook/0596001673/…
-
rejectedButton= tk.Button(warningscreen, text='Stop',command=warningscreen.destroy)
标签: python function user-interface command exit